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

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -