模型:
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 => trueend
控制器:
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
嵌套模型:
class MatchTeam < ActiveRecord::Base belongs_to :match belongs_to :teamend
协会:
class Team < ActiveRecord::Base has_and_belongs_to_many :matchesend
视图:
<%= 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 > <%= f.label :winning_team_ID %><br /> <%= f.collection_select :winning_team_ID,@match.teams,:representation %> </div> <% end %> <div > <%= f.submit %> </div><% end %>
ParaMS:
Processing by MatchesController#update as HTMLParameters: {"utf8"=>"Ô£ô","authenticity_token"=>"QIJChzkYOPZ1hxbzTZS8H3AXc7iBzkKv3Z5darmlOsQ=","match"=>{"match_teams_attributes"=>{"0"=>{"team_ID"=>"1","ID"=>""},"1"=>{"team_ID"=>"3","ID"=>""}},"winning_team_ID"=>"3"},"commit"=>"Update Match","ID"=>"2"}
创建与2个团队的新匹配工作正常,编辑视图也显示正确的值,但更新 *** 作给我这个错误.
undefined method `to_sym' for nil:NilClassapp/controllers/matches_controller.rb:65:in `block in update'line 65: if @match.update_attributes(params[:match])解决方法 我已经明白了.我读到像MatchTeams这样的连接表不需要ID.我猜这不是做任何嵌套表单时都是如此.我重新删除我的迁移删除了ID列的排除,现在一切正常.难道我们都不喜欢这个愚蠢的错误吗? 总结
以上是内存溢出为你收集整理的ruby-on-rails – Rails:更新嵌套属性 – nil的未定义方法`to_sym’:NilClass全部内容,希望文章能够帮你解决ruby-on-rails – Rails:更新嵌套属性 – nil的未定义方法`to_sym’:NilClass所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)