Not getting the "front" variable for the flashcard app

Hi there, I’m on the part where Matthew let us figure out the part where we implement our own restful routes to the Card Model. I’m on the part that is creating a new card, but I can’t seem to get the “front” value, but I could get the “back” value in my model. Here’s my current codes.

Cards Controller

class CardsController < ApplicationController
  before_action :deck_params_id

  def show
    @card = @deck.cards.find(params[:id])
  end

  def new
    @card = @deck.cards.new
  end

  def create
    @card = @deck.cards.new(card_params)
    @card.save

    redirect_to @deck
  end

  private
  def deck_params_id 
    @deck = Deck.find(params[:deck_id])
  end

  def card_params
    params.require(:card).permit(:front)
    params.require(:card).permit(:back)
  end
end

New Form View

<%= form_for [@deck, @card] do |f| %>
  <div>
    <%= f.label :front %>
    <%= f.text_field :front %>
  </div>

  <div>
    <%= f.label :back %>
    <%= f.text_field :back %>
  </div>

  <%= f.submit "Create new card" %>
<% end %>

Routes

     Prefix Verb   URI Pattern                              Controller#Action
    deck_cards POST   /decks/:deck_id/cards(.:format)          cards#create
 new_deck_card GET    /decks/:deck_id/cards/new(.:format)      cards#new
edit_deck_card GET    /decks/:deck_id/cards/:id/edit(.:format) cards#edit
     deck_card GET    /decks/:deck_id/cards/:id(.:format)      cards#show
               PATCH  /decks/:deck_id/cards/:id(.:format)      cards#update
               PUT    /decks/:deck_id/cards/:id(.:format)      cards#update
               DELETE /decks/:deck_id/cards/:id(.:format)      cards#destroy
         decks GET    /decks(.:format)                         decks#index
               POST   /decks(.:format)                         decks#create
      new_deck GET    /decks/new(.:format)                     decks#new
     edit_deck GET    /decks/:id/edit(.:format)                decks#edit
          deck GET    /decks/:id(.:format)                     decks#show
               PATCH  /decks/:id(.:format)                     decks#update
               PUT    /decks/:id(.:format)                     decks#update
               DELETE /decks/:id(.:format)                     decks#destroy

My results when I add a byebug method before saving the card is:

byebug) @card
#<Card id: nil, front: nil, back: "Barney", deck_id: 19, created_at: nil, updated_at: nil>
(byebug) params
{"utf8"=>"✓", "authenticity_token"=>"t/5vLn5zlui6A4ui4kYx+kAgXWEamqMTQu6KCWSM7e/mFJVekOLcZMc/bPkXn7zu/ibhqsu2wXsMT8yqOASNLw==", "card"=>{"front"=>"Dinosaur", "back"=>"Barney"}, "commit"=>"Create new card", "controller"=>"cards", "action"=>"create", "deck_id"=>"19"}
(byebug) card_params
Unpermitted parameter: back
Unpermitted parameter: front
{"back"=>"Barney"}
(byebug)

Seems wierd. Since I have an awesome forum to ask, I hope you don’t find it stupid :slight_smile: Thanks!

SOLVED!

Just change

def card_params
    params.require(:card).permit(:front)
    params.require(:card).permit(:back)
  end 

to

  def card_params
    params.require(:card).permit(:front, :back)
  end

although I don’t know the reason behind it, I just look at how it can be write in one line.