【发布时间】:2016-03-31 13:57:39
【问题描述】:
我有一个 Post 和一个 MaterielLink 模型。
一个帖子has_many materiel_links 和accepts_nested_attributes_for :materiel_links
我使用 gem cocoon 创建一个嵌套表单:在 post 表单上,我希望能够添加将在提交表单时创建的链接。
post/new.html.erb:
<%= simple_form_for @post, :html => { :id => "post_form", "data-post-id" => @post.id } do |f| %>
<%= f.simple_fields_for :materiel_links do |materiel_link| %>
<%= render 'materiel_link_fields', f: materiel_link %>
<% end %>
<%= link_to_add_association 'Ajouter', f, :materiel_links%>
<% end %>
_materiel_link_fields.html.erb:
<%= f.fields_for :materiel_links do |materiel_link| %>
<%= materiel_link.text_field :name %>
<%= materiel_link.text_field :link %>
<% end %>
在我的后期控制器中:
def update
@materiel_links = @post.materiel_links.build(post_params[:materiel_links_attributes]
if @post.update!(post_params)
session[:current_draft_post_id] = nil
redirect_to post_path(@post)
else
render :new
end
end
我在这里进行更新操作,因为由于特定于我的 rails 应用程序的原因,帖子是在呈现帖子/新页面时创建的(它被创建为空,用户只是更新它而不是实际创建它) .所以帖子已经存在,但不是我必须在更新操作中创建的 materiel_links。
还有参数:
def post_params
params.require(:post).permit(:title, materiel_links_attributes: [:name,:link] )
end
我在更新操作中添加了一个 raise,但奇怪的是,当我输入 params 时,我可以找到我添加的每个 materiel_link 的链接/名称,但每对之前都有一个数字:
>> params
{"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"wqzWfaAcwrOOdxViYBO5HaV2bwsNsf5HsvDFEbBYapkOMAPXOJR7oT4zQHbc/hTW8T9a+iH5NRl1WUApxrIjkA==", "post"=>{"title"=>"my title", "materiel_links_attributes"=>{"1459431927732"=>{"materiel_links"=>{"name"=>"mon Lien 1", "link"=>"htttp1"}}, "1459431933881"=>{"materiel_links"=>{"name"=>" Mon lien 2", "link"=>"htttp2"}}}}, "controller"=>"posts", "action"=>"update", "id"=>"1250"}
但是当我输入 post_params 时, materiel_links 哈希中没有任何内容:
>> post_params
=> {"title"=>"my title","materiel_links_attributes"=>{"1459431927732"=>{}, "1459431933881"=>{}}}
MaterielLink 的实例已创建,但它们是空的:它们不保存链接/名称。
我哪里做错了?
【问题讨论】:
-
有
materiel_link have_many materiel_links吗?
标签: ruby-on-rails forms nested-forms cocoon-gem