Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 link_to routes (edit) nested resources

Sorry if this has been asked elsewhere, but I can't figure this out. I have a forum with sections, topics, and replies. I'm trying to edit and delete replies from the show topic view. This is the structure:

resources :sections do
  resources :topics do
    resources :replies
  end
end

So I do a rake routes to see where I'm linking my edit reply. I see that its edit_section_topic_reply and in my link_to I add _path to it. Now this is what I can't figure out. What parameters am I passing it? Shouldn't it be:

<%= link_to 'Edit', edit_section_topic_reply_path(@reply, @topic, @section) %>

I get a ActionController::RoutingError in Topics#show when I do this.

No route matches {:topic_id=>#<Topic id: 2, section_id: 2, user_id: nil, subject: "subject", body: "body", created_at: "2011-03-04 08:37:37", updated_at: "2011-03-04 21:37:16">, :controller=>"replies", :action=>"edit", :section_id=>nil, :id=>#<Section id: 2, name: "Section", description: "Section Description", created_at: "2011-03-04 07:50:56", updated_at: "2011-03-04 07:50:56">}

It seems like it isn't passing IDs, but the nest before, my new topic works fine

new_section_topic_reply_path(@topic, @section)
like image 851
a3uge Avatar asked Mar 04 '11 23:03

a3uge


2 Answers

I really dislike this aspect of the link_to helper. In the interest of making your code more readable and less prone to error, I would suggest that you be explicit about which IDs you are passing in.

<%= link_to 'Edit', edit_section_topic_reply_path(:id => @reply.id, 
                                                  :topic_id => @topic.id, 
                                                  :section_id => @section.id) %>

I've run into too many subtle and seemingly insane bugs due to params being out of order in a link_to.

like image 162
jdl Avatar answered Nov 19 '22 06:11

jdl


One more way for edit link

<%= link_to [:edit,@section,@topic,@reply] %>
like image 30
Saraswathy Renuga Avatar answered Nov 19 '22 05:11

Saraswathy Renuga