Rails: updating nested attributes - undefined `to_sym 'method for nil: NilClass

Edit: Added update action and on which line the error occurs

Model:

class Match < ActiveRecord::Base has_and_belongs_to_many :teams has_many :match_teams has_many :teams, :through => :match_teams accepts_nested_attributes_for :match_teams, :allow_destroy => true end 

Controller:

  def new @match = Match.new @match_teams = 2.times do @match.match_teams.build end respond_to do |format| format.html # new.html.erb format.json { render json: @match } end end def update @match = Match.find(params[:id]) respond_to do |format| if @match.update_attributes(params[:match]) format.html { redirect_to @match, notice: 'Match was successfully updated.' } format.json { head :ok } else format.html { render action: "edit" } format.json { render json: @match.errors, status: :unprocessable_entity } end end end 

Nested Model:

 class MatchTeam < ActiveRecord::Base belongs_to :match belongs_to :team end 

Association:

 class Team < ActiveRecord::Base has_and_belongs_to_many :matches end 

View:

 <%= form_for(@match) do |f| %> <%= f.fields_for :match_teams, @match_teams do |builder| %> <%= builder.collection_select :team_id, Team.all, :id, :name, :include_blank => true %> <% end %> <% unless @match.new_record? %> <div class="field"> <%= f.label :winning_team_id %><br /> <%= f.collection_select :winning_team_id, @match.teams, :id, :representation %> </div> <% end %> <div class="actions"> <%= f.submit %> </div> <% end %> 

Params:

 Processing by MatchesController#update as HTML Parameters: {"utf8"=>"ร”ยฃรด", "authenticity_token"=>"QIJChzkYOPZ1hxbzTZS8H3AXc7i BzkKv3Z5daRmlOsQ=", "match"=>{"match_teams_attributes"=>{"0"=>{"team_id"=>"1", " id"=>""}, "1"=>{"team_id"=>"3", "id"=>""}}, "winning_team_id"=>"3"}, "commit"=>" Update Match", "id"=>"2"} 

Creating a new match with two teams works fine, the edit view also shows the correct values, but the update action gives me this error.

 undefined method `to_sym' for nil:NilClass app/controllers/matches_controller.rb:65:in `block in update' line 65: if @match.update_attributes(params[:match]) 
+4
source share
2 answers

I get it. I read that a join table like MatchTeams does not require an identifier. I assume that this is true if you do not do nested forms. I suffered migration except for the id column exception, and now everything is working fine. Don't we like these stupid mistakes? :)

+8
source

Without seeing the offensive to_sym in your code, just know that the item to which it is attached was not correctly defined. If it is a variable of type @var.to_sym , you most likely:

  • Do not install @var at all
  • Ask it, but it returns nil because there are no matches (e.g. @var = @project.companies.first , but @project not tied to it by anyone).
  • Your params missing the corresponding data bit. If your to_sym relies on the data submitted through the form, this will not work unless the user accepts the data bit that you are expecting. In this case, you first need to check whether the data was entered before running .to_sym on it.
0
source

All Articles