Ruby on rails 不能';找不到ID为2的团队进行ID匹配=
试图建立多对多关系 我有两个模型,团队和比赛,我正在做一个多对多关系,但在试图保存嵌套模型时出现了这个错误 '找不到ID为2的团队进行ID为的匹配=' 我知道我的模型是新的,所以它还没有id,但我正在使用Match.new(params[:Match]方法,应该可以工作Ruby on rails 不能';找不到ID为2的团队进行ID匹配=,ruby-on-rails,Ruby On Rails,试图建立多对多关系 我有两个模型,团队和比赛,我正在做一个多对多关系,但在试图保存嵌套模型时出现了这个错误 '找不到ID为2的团队进行ID为的匹配=' 我知道我的模型是新的,所以它还没有id,但我正在使用Match.new(params[:Match]方法,应该可以工作 Team: class Team < ActiveRecord::Base attr_accessible :description, :name, :status attr_protected :id
Team:
class Team < ActiveRecord::Base
attr_accessible :description, :name, :status
attr_protected :id
has_many :matchships
has_many :matches, :through => :matchships
end
Match:
class Match < ActiveRecord::Base
attr_accessible :date, :name
attr_protected :id
has_many :matchships
has_many :teams , :through => :matchships
accepts_nested_attributes_for :teams
end
MatchShips:
class Matchship < ActiveRecord::Base
attr_accessible :match_id, :team_id
belongs_to :match
belongs_to :team
end
Match Controller:
New:
def new
@match = Match.new
@match.teams.build
end
Create:
def create
'failing here' --------> @match = Match.new(params[:match])
@team = Team.find(params[:team_id])
@match.teams << @team
@team.matches << @match
团队:
类团队:匹配
结束
匹配:
类匹配:matchships
接受:团队的\u嵌套\u属性\u
结束
配对:
类匹配@match=match.new(参数[:match])
@team=team.find(参数[:team\u id])
@比赛队
{:id=>“#{form_tag_id(builder.object_name,:id)}”},:class=>“input small”,:placeholder=>“Search”%>
“btn btn小型btn主节点”%>
控制器您可以向我们展示您试图保存嵌套模型时使用的表单和控制器操作吗?关联似乎已正确配置。为什么您的模型中有attr\u protected:id?因为您将得到以下结果:无法批量分配受保护的属性:teams\u属性
<%= nested_form_for(@match) 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 class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= 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 class="icon-plus-sign"></i>'), :teams, :class => 'btn btn-small btn-primary' %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>