We are currently creating forms for models in hierarchical relationships.
Below is the model file.
post.rb
has_many:items
accepts_nested_attributes_for —items
item.rb
has_one:heading, dependent::destroy
has_one:content,dependent::destroy
has_one:link,dependent::destroy
has_one:movie,dependent::destroy
has_one:photo,dependent::destroy
has_one:quate,dependent::destroy
accepts_nested_attributes_for —heading
accepts_nested_attributes_for:content
accepts_nested_attributes_for —link
accepts_nested_attributes_for —movie
accepts_nested_attributes_for —photo
accepts_nested_attributes_for:quate
heading, content, link, movie, photo, quote.rb
(This means that there are heading.rb, conten.rb, and link.rb respectively.And all of them have the following settings in common.)
belongs_to:item
posts_controller.rb
def new
@post=current_user.posts.build
end
def create
@post=current_user.posts.build(post_params)
response_to do | format |
[email protected]
format.html {redirect_to@post,notice: 'created!!'}
else
format.html {render:new}
end
end
def post_params
param.require(:post).permit(:title,:description,:image,:user_id,
items_attributes: [:id,:order,
heading_attributes: [:id,:head],
photo_attributes: [:id,:image,:title,:q_url],
movie_attributes: [:id,:y_url],
quote_attributes: [:id,:quate,:q_url,:q_title,:q_comment],
content_attributes: [:id,:content],
link_attributes: [:id,:url,:l_text],
twitter_attributes: [:id,:t_url]
])
end
posts/new_and_edit.html.erb
<%=form_for(@post)do|f|%>
<%[email protected] y?%>
<ul>
<%@post.errors.full_messages.each do | msg|%>
<li><%=msg%></li>
<%end%>
</ul>
<%end%>
<%=f.label: title%>
<%=f.text_field: title%>
<%=f.label:description%>
<%=f.text_field:description%>
<%=f.label: image%>
<%=f.file_field: image%>
<%=f.hidden_field: user_id%>
<%=f.submit%>
<%=render'posts/item_form_fields', name: 'headings'%>
<%=render 'posts/item_form_fields', name: 'contents'%>
<%=render'posts/item_form_fields', name: 'movies'%>
<%=render'posts/item_form_fields', name: 'quates'%>
<%=render'posts/item_form_fields', name: 'links'%>
<%=render'posts/item_form_fields', name: 'photos'%>
posts/_item_form_fields.html.erb
<%=form_for(@post,remote:false)do|m|%>
<%=render 'items/form_fields', m:m, name:name%>
<%=m.submit "Create"%>
<%end%>
items/_form_fields.html.erb
<%=m.fields_for:items,@item do|b|%>
<%=b.hidden_field:order,value:'0'%>
<%=render "#{name}/form_fields", b:b%>
<%end%>
headings/_form_fields.html.erb
<%=b.fields_for:heading, @item.build_heading do | h | %>
<%=h.text_field:head%>
<%end%>
As mentioned above, I have _form_fields.html.erb for the content, link, movie, photo, and quote tables respectively.
When running in the current local environment, child related forms (heading, content, etc.) are not displayed."Also, if you click ""Create"", the post_params of the post_controller will display the following error:
"param is missing or the value is empty:post
Could someone help me?
ruby-on-rails ruby rails-activerecord
Why don't you set @post=current_user.posts.build
to @post=current_user.posts.items.build
in the new action?
For more information, see "Fields_for disappear when adding acceptances_nested_attributes_for"
© 2024 OneMinuteCode. All rights reserved.