1

I am using a fields_for form to create a selection input form for x amount of associated items. In this case, it's games that are associated with an entry. I want to let the user choose a winner for each game in the entry and store their selection. This is my form.

<%= simple_form_for [@contest, @entry] do |f| %>

...some fields, then...

<div class="form-group">
<%= f.label :games %><br>
<% Contest.find_by_id(params[:contest_id]) do |contest| %>
<%= f.simple_fields_for :entry_selections do |selection| %>    
  <table>
    <table class="table table-half">
      <thead>
        <tr>
          <th>Away</th>
          <th>Home</th>
          <th>Choose Winner</th>    
        </tr>
      </thead>
        <tbody>
        <% contest.matchset.games.each do |game| %>
          <tr>
            <td><%= game.away_team_name %></td> 
            <td><%= game.home_team_name %></td>
            <td><%= selection.input :team_selected, collection: [[game.away_team_id, game.away_team_name], [game.home_team_id, game.home_team_name]], :label_method => :last, :value_method => :first, :required => false, label: false %></td>
          </tr>
        <% end %>
        <% end %> 
      </tbody>
      </table>
  <% end %>
</div>

I am trying to save each :team_selected on a model called entry_selections. I think the reason is because it can't create a new record on there for some reason. This is from the server log.

Processing by EntriesController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"HniBF1MQyBdgcNT71vAfyGr4BuJ+OlPBENmVo7FN6hc=", "entry"=>{"name"=>"", "user_id"=>"1", "contest_id"=>"22", "entry_selections"=>{"team_selected"=>"1"}}, "commit"=>"Update Entry", "id"=>"12"}
  Entry Load (0.2ms)  SELECT "entries".* FROM "entries" WHERE "entries"."id" = ? LIMIT 1  [["id", "12"]]
Unpermitted parameters: entry_selections

I have also added this to the entries_controller.

def entry_params
  params.require(:entry).permit(:name, :user_id, :contest_id, :matchset_id, entry_selections_attributes: [:game_id, :team_selected, :game_weight])
end

My Entries model has_many :entry_selections and my EntrySelections model belongs_to :entry

Does anyone have any advice on how to store the users team selection for each game?

EDIT: adding contest model and entry_selections model

contest.rb

class Contest < ActiveRecord::Base
has_many :entries

...

validates :league_name, :league_id, :matchset_id, :size, presence: true
end

entry_selection.rb

class EntrySelection < ActiveRecord::Base
belongs_to :entry

validates :team_selected, presence: true

end
mcnollster
  • 537
  • 1
  • 4
  • 14

1 Answers1

0

I'm still new to rails but should this be correct:

entry_selections_attributes:

Should be

entry_selections

Shaun Frost Duke Jackson
  • 1,256
  • 1
  • 10
  • 22
  • are you sure? I get an error on the controller if I change it to that. I was referencing this post where someone said that adding it the way I have it fixed their solution - http://stackoverflow.com/questions/15919761/rails-4-nested-attributes-unpermitted-parameters – mcnollster Nov 21 '13 at 18:37