1

Im trying to render a form from another controller in my view.

This is posts_index, and the render post.comments works fine, but the form for a new comment doesnt.

<% @posts.each do |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 get this error: undefined method `comments' for nil:NilClass

The comments form:

<%= form_for([@post, @post.comments.build]) do |f| %>
  <%= f.label :Comment %><br />
  <%= f.text_area :body, :class => "comment_text_box" %>
  <%= f.submit %>
<% end %>

I understand I need to pass the post.comments to the form, but can't figure out how.

Post_show works with <%= render "comments/form" %>

Post and comments are a has_many relationship, posts has_many comments.

Thanks for looking.

NorCalKnockOut
  • 870
  • 3
  • 10
  • 26

2 Answers2

1

You need to pass variables into your partial like this:

<% @posts.each do |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 change your form partial to this:

<%= form_for([post, post.comments.build]) do |f| %>
  // form fields
<% end %>
Mandeep
  • 9,093
  • 2
  • 26
  • 36
  • So i just tried this, and now im getting: undefined local variable or method `post' for #<#:0x007ffa5435c978> – NorCalKnockOut Oct 06 '14 at 15:56
  • 1
    @user2891803 did you pass post variable in partial? – Mandeep Oct 06 '14 at 15:58
  • 1
    @user2891803 try updated answer. If it doesn't work then can you post full error message and on which line do you get this error? – Mandeep Oct 06 '14 at 16:03
  • I tried again, the error is one line 1: undefined local variable or method `post' for #<#:0x007ffa53eef9a8> Extracted source (around line #1): 1: <%= form_for([post, post.comments.build]) do |f| %> .... – NorCalKnockOut Oct 06 '14 at 16:05
  • If it helps, the code you provided works in the post_show page. – NorCalKnockOut Oct 06 '14 at 16:08
  • 1
    @user2891803 Strange because we are passing post variable in partial by `locals: {post: post}`. May be browser is caching it. Can you restart server or try it in different browser? What is the difference between post_show page? – Mandeep Oct 06 '14 at 16:10
  • scratch that, it doesnt work on show page, unless i change post back to `@post`... and in the comments create, i have: `@post = Post.find(params(:post_id))` – NorCalKnockOut Oct 06 '14 at 16:13
  • 1
    @user2891803 can you edit your question and post posts index view the way it is? – Mandeep Oct 06 '14 at 16:15
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/62531/discussion-between-mandeep-and-user2891803). – Mandeep Oct 06 '14 at 16:21
0

When you ask for the partial, you need to send it the post it's related to. It would look like this:

<% @posts.each do |post| %>
  <%= link_to post.title, post %>
    <%= simple_format post.text %>
      <%= render post.comments.order('created_at DESC').all %>
      <%= render :partial => '/comments/form', post: post%>
Anthony
  • 15,435
  • 4
  • 39
  • 69