Ruby-on-rails accepts_nested_attributes_for with has_many => :through 选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2212622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
accepts_nested_attributes_for with has_many => :through Options
提问by Andrew C
I have two models, links and tags, associated through a third, link_tags. The following code is in my Link model.
我有两个模型,链接和标签,通过第三个,link_tags 关联。以下代码在我的 Link 模型中。
Associations:
协会:
class Link < ActiveRecord::Base
has_many :tags, :through => :link_tags
has_many :link_tags
accepts_nested_attributes_for :tags, :allow_destroy => :false,
:reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
end
class Tag < ActiveRecord::Base
has_many :links, :through => :link_tags
has_many :link_tags
end
class LinkTag < ActiveRecord::Base
belongs_to :link
belongs_to :tag
end
links_controller Actions:
links_controller 操作:
def new
@link = @current_user.links.build
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @link }
end
end
def create
@link = @current_user.links.build(params[:link])
respond_to do |format|
if @link.save
flash[:notice] = 'Link was successfully created.'
format.html { redirect_to links_path }
format.xml { render :xml => @link, :status => :created, :location => @link }
else
format.html { render :action => "new" }
format.xml { render :xml => @link.errors, :status => :unprocessable_entity }
end
end
end
View code from new.html.erb:
从 new.html.erb 查看代码:
<% form_for [current_user, @link], :url => account_links_path do |f| %>
<%= render :partial => "form", :locals => { :f => f } %>
<% end %>
And the corresponding partial:
以及相应的部分:
<%= f.error_messages %>
<p>
<%= f.label :uri %><br />
<%= f.text_field :uri %>
</p>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<h2>Tags</h2>
<% f.fields_for :tags_attributes do |tag_form| %>
<p>
<%= tag_form.label :name, 'Tag:' %>
<%= tag_form.text_field :name %>
</p>
<% unless tag_form.object.nil? || tag_form.object.new_record? %>
<p>
<%= tag_form.label :_delete, 'Remove:' %>
<%= tag_form.check_box :_delete %>
</p>
<% end %>
<% end %>
<p>
<%= f.submit 'Update' %>
</p>
The following line of code, in the create action in the Link controller throws an error:
下面这行代码,在 Link 控制器的 create 动作中抛出了一个错误:
@link = @current_user.links.build(params[:link])
The error: Tag(#-621698598) expected, got Array(#-609734898)
错误: Tag(#-621698598) expected, got Array(#-609734898)
Are there additional steps needed in the has_many => :through case? These seem to be the only indicated changes for the basic has_many case.
在 has_many => :through 案例中是否需要额外的步骤?这些似乎是基本 has_many 情况的唯一指示更改。
回答by Jyothu
Take a look at the line of your code
看看你的代码行
<% f.fields_for :tags_attributes do |tag_form| %>
You need to use just :tagsinstead of :tags_attributes.
This will solve your issue
您需要使用 just:tags而不是:tags_attributes. 这将解决您的问题
Make sure that You have build links and tags in your controller like
确保您在控制器中构建了链接和标签,例如
def new
@link = @current_user.links.build
@link.tags.build
end
回答by Tiago
I found this here on stackoverflow:
我在stackoverflow上找到了这个:
Rails nested form with has_many :through, how to edit attributes of join model?
带有 has_many 的 Rails 嵌套表单:通过,如何编辑连接模型的属性?
please tell me if it worked.
请告诉我它是否有效。
回答by jaredonline
In order for this to work, you need to pass in the right params hash:
为了使其工作,您需要传入正确的 params 哈希:
params = {
:link => {
:tags_attributes => [
{:tag_one_attr => ...}, {:tag_two_attr => ...}
],
:link_attr => ...
}
}
And your controller will look like:
你的控制器看起来像:
def create
@link = Link.create(params[:link]) # this will automatically build the rest for your
end
回答by obiwanchinobi
Try this:
尝试这个:
<% f.fields_for :tags_attributes do |tag_form| %>
回答by Steve
In your controller in the new action (that loads the form partial), are you building a @tag through your link?
在您的控制器中的新操作(加载表单部分)中,您是否通过链接构建了 @tag?
So you should see something along the lines of:
所以你应该看到一些类似的东西:
@link = Link.new
@tag = @link.tags.build
It might be best to post the contents of the new and create action of your links_controller.
最好发布链接控制器的新内容和创建操作。
回答by soheildb
try
尝试
<% f.fields_for :tags do |tag_form| %>
(ie lose the _attributes in :tag_attributes) That's how I've usually done nested forms
(即丢失 :tag_attributes 中的 _attributes)这就是我通常完成嵌套表单的方式
回答by Aaron Rentheitroad
You need to build a tag in your controller or in the view
您需要在控制器或视图中构建标签
def new
@link = @current_user.links.build
@link.tags.build
end
#in your view you can just use the association name
<% f.fields_for :tags do |tag_form| %>

