
我有以下帖子参数
{"utf8"=>"✓","_method"=>"put","authenticity_token"=>"knq4dG1U/5NJxMD6KYxfOpKd3CuOBHRlp6xCwdpwCnQ=","match"=>{"name"=>"latest match","date(1i)"=>"2013","date(2i)"=>"5","date(3i)"=>"19","teams_attributes"=>{"1368967240149"=>{"name"=>"Navi","ID"=>"1"}}},"commit"=>"Update Match","match_ID"=>"2"} 模型:
team has_many :matchipsteam has_many :matches :through => matchipsmatch has_many :matchipsmatch has_many :teams :through => matchips
团队控制器:
def create @team = Team.find(params[:team_ID]) <-----fails here! redirect_to @match end
形成:
<%= nested_form_for @match,:url => {:action => "add_team_to_match_post"} do |f| %> <% if @match.errors.any? %> <div ID="error_explanation"> <h2><%= pluralize(@match.errors.count,"error") %> prohibited this match from being saved:</h2> <ul> <% @match.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <div > <%= f.label :name %><br /> <%= f.text_fIEld :name %> </div> <div > <%= f.label :date %><br /> <%= f.date_select :date %> </div> <%= f.fIElds_for :teams,:HTML => { :class => 'form-vertical' } do |builder| %> <%= builder.label "Team name:" %> <%= builder.autocomplete_fIEld :name,autocomplete_team_name_teams_path,:update_elements => {:ID => "##{form_tag_ID(builder.object_name,:ID)}" },:class => "input-small",:placeholder => "Search" %> <%= builder.hIDden_fIEld :ID %> <% end %> <%= f.link_to_add raw('<i ></i>'),:teams,:class => 'btn btn-small btn-primary' %> </div> <div > <%= f.submit %> </div><% end %> 所以我基本上在比赛和球队之间有多对多的关系.使用nested_form进行匹配,但问题是它在创建或更新之前正在寻找关联,我想要它.我以前使用多对一关联完成此 *** 作,其中子模型将在创建或更新父项时创建,在这种情况下我已经拥有所有我正在做的团队通过post / put请求传递ID和名称所以我可以将它与那场比赛联系起来.
如果有更好的方法来关联已创建的实体,请告诉我.
解决方法 你可能需要这样的东西:def add_team_to_match_post @match = Match.find(params[:match_ID]) params[:teams_attributes].each_value do |team_attributes| team = Team.find(team_attributes[:ID]) @match.teams << team team.matches << @match end redirect_to @matchend
基本上,这是遍历teams_attributes哈希中的每个团队并将其添加到匹配对象(因为这是多对多)
总结以上是内存溢出为你收集整理的ruby – rails post params没有找到id全部内容,希望文章能够帮你解决ruby – rails post params没有找到id所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)