2

I am experimenting with nested resources:

My routes:

  resources :conversations do
    resources :replies do
      resources :comments
    end
  end

I was able to get the form for replies to work with the conversation, but now I'm adding the additional complexity of getting comments to work with replies.

The entirety of the forms are all under the conversation show path.

<%= form_for([@conversation, @reply]) do |f| %>
    <%= render 'shared/response_form', f: f %>
    <%= f.submit "Reply", class: "btn btn-large btn-primary" %>
<% end %>

The above form for replies works fine and gets no errors, the below form for comments gets an error:

undefined method `reply_comments_path'

<%= form_for([@reply, @comment]) do |f| %>
    <%= render 'shared/response_form', f: f %>
    <%= f.submit "Comment", class: "btn btn-large btn-primary" %>
<% end %>

Here is my conversations controller for show, this is where I think the problem is:

  def show
    @conversation = Conversation.find(params[:id])
    @replies = @conversation.replies
    @reply = current_user.replies.build
    #If I change the above line to @conversations.replies.build 
    #it breaks the ability to show replies above the form.

    @comments = @reply.comments
    @comment = @reply.comments.build    
  end

However, someone else suggested doing this:

<%= form_for([@conversation, @reply, @comment]) do |f| %>
    <%= render 'shared/response_form', f: f %>
    <%= f.submit "Comment", class: "btn btn-large btn-primary" %>
<% end %>

But it only ended up with a routing error:

No route matches {:controller=>"comments", :format=>nil, :conversation_id=>#<Conversation id: 3, content: "Goes here.", user_id: 1, created_at: "2012-12-10 21:20:01", updated_at: "2012-12-10 21:20:01", subject: "Another conversation">, :reply_id=>#<Reply id: nil, content: nil, user_id: 1, created_at: nil, updated_at: nil, conversation_id: nil>}

I always get this undefined method path error when I try making new forms and I always manage to forget what I did wrong. The answer never seems to be the routes.

EDIT:

Under the create section in the controller I have:

@replies = @conversation.replies
@reply = current_user.replies.build
#If I change the above line to @conversations.replies.build 
#it breaks the ability to show replies above the form.

I have no idea why @reply = @conversation.replies.build breaks the ability to show the existing replies. I get an error saying it can't convert nil to a number, and can't see the reply.created_at or reply.content. Whatever is causing that may be a clue as to why I'm having this problem. However, in the replies controller I AM using

@reply = conversation.replies.build(content: params[:reply][:content], user_id: current_user.id)

EDIT:

Just to add, Stackoverflow does something very similar to what I'm trying to achieve here, except that you can comment on the questions as well as the answers.

Nathan McKaskle
  • 2,926
  • 12
  • 55
  • 93

1 Answers1

3

Look at the end of your error:

... :reply_id=>#<Reply id: nil, content: nil, user_id: 1, created_at: nil, updated_at: nil, conversation_id: nil>}

You can't create a form for @comment if @reply is not saved. You need to persist @reply before create a @comment to it.

If you haven't validates on Reply model try this simple test on show action:

# @reply = current_user.replies.build
@reply = current_user.replies.create

See comments for answer.

Nathan McKaskle
  • 2,926
  • 12
  • 55
  • 93
daniloisr
  • 1,367
  • 11
  • 20
  • Thing is the form only shows <% if @replies.any? %>, so it shouldn't even be showing on a conversation with no replies. – Nathan McKaskle Dec 11 '12 at 19:02
  • I tested this, it definitely doesn't show the form or get any errors if there are no replies. – Nathan McKaskle Dec 11 '12 at 19:09
  • I re-read your question and I be on doubt, you **want** to create a reply/comment without a conversation? If not, why you used `current_user.replies.build`? – daniloisr Dec 11 '12 at 19:15
  • That's the thing, if you see the comments I made below that line, I have no idea why that works. In the replies controller under create I have @conversation.replies.build(etc.. I have no idea why it won't let me do what you're saying without breaking the ability to display existing replies. Whatever is causing that may be the key to why this isn't working. – Nathan McKaskle Dec 11 '12 at 19:18
  • You have any validations on Reply model? – daniloisr Dec 11 '12 at 19:33
  • Yes, it validates the existence of a user_id, conversation_id and content. – Nathan McKaskle Dec 11 '12 at 19:40
  • So to `@conversations.replies.build` works you will need to specify user_id and content. Try `@reply = @conversations.replies.create(:user => current_user, :content => 'any')`, I put `'any'` on content just as a test, because the question is that the `@reply` object must exist to you use it to get a path like `[@conversation, @reply, @comment]`. If yout want to create both objects (@reply and @comment) with a single form you will need to use [nest_forms](http://railscasts.com/episodes/196-nested-model-form-part-1) – daniloisr Dec 11 '12 at 19:51
  • The first suggestion worked. The text area field has the 'any' text in it. It works, but not sure it's the ideal way to do it. Maybe if I take the label off and just put 'Reply here' in place of 'any' and put some java in later that removes that text when you enter the field. I dunno. Thanks! – Nathan McKaskle Dec 11 '12 at 20:10