ruby on rails - Render form partial from view ROR -
ruby on rails - Render form partial from view ROR -
im trying render form controller in view.
this posts_index, , render post.comments works fine, form new comment doesnt.
<% @posts.each |post| %> <%= link_to post.title, post %> <%= simple_format post.text %> <%= render post.comments.order('created_at desc').all %> <%= render :partial => '/comments/form', locals: {post: post} %>
i error: undefined method `comments' nil:nilclass
the comments form:
<%= form_for([@post, @post.comments.build]) |f| %> <%= f.label :comment %><br /> <%= f.text_area :body, :class => "comment_text_box" %> <%= f.submit %> <% end %>
i understand need pass post.comments form, can't figure out how.
post_show works <%= render "comments/form" %>
post , comments has_many
relationship, posts has_many
comments.
thanks looking.
you need pass variables partial this:
<% @posts.each |post| %> <%= link_to post.title, post %> <%= simple_format post.text %> <%= render post.comments.order('created_at desc').all %> <%= render partial: '/comments/form', locals: {post: post} %> <% end %>
and alter form partial this:
<%= form_for([post, post.comments.build]) |f| %> // form fields <% end %>
ruby-on-rails ruby forms partial
Comments
Post a Comment