Domanda

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.

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top