Rails 3.0.9: edit_resource_path refers to parent resource in recursive associations #1919

Closed
jthemphill opened this Issue Jun 30, 2011 · 9 comments

Projects

None yet

3 participants

@jthemphill

This bug is fairly situational; here's some fictionalized background on my project.

I've set up a resource; let's call them Nodes. A Node has_many children (:classname "Node") and belongs_to a parent (:classname "Node"). So it's a recursive, treelike association.

The top-level parents are listed in a table which leverages the partial "/shared/node". The show action for each node displays its children using the same partial.

Here's where the bug comes in: I've added an edit_node_path link to the Node partial. However, with the exception of the top-level Nodes, each edit_node_path refers to the parent of the Node I actually want to edit! This is odd and inconsistent, particularly since Show and Destroy refer to the correct node.

@jthemphill

Specifying the node with edit_node_path(node) will link to the correct node. It still seems as though Rails should do this by default, though.

@bhus

How were you calling it in the first place? Doesn't edit_node_path require an argument?

@jthemphill

No, edit_node_path doesn't require an argument.

Let me see if I can make the problem clearer...

Imagine I have two resources: Tree and Node.

In the show view for Tree, I render a collection of Nodes using /shared/_node.html.erb, which looks something like this:

<li>
  <ul>
    <li><%= link_to "Show", node %></li>
    <li><%= link_to "Edit", edit_node_path %></li>
    <li><%= link_to "Destroy", node, :method => "delete" %></li>
  </ul>
</li>

Each of these, including edit, takes me to the correct page for the Node being rendered by the partial. So far, so good.

Now imagine I call the show action for Node A. The show action for a Node also renders a collection of Node partials using the same view.

Nodes D and E are children of Node A, so from A's show page I can see Nodes D and E, each with the same three links as I saw for A. The show and delete links for D and E are correct, but clicking the edit links for either D or E will take me to the edit action for A!

Since edit_node_path by default works correctly for Node A (but not D and E), I believe that, when not given arguments, edit_node_path will first try to link to the resource referenced by the variable @node. If @node is nil, edit_node_path will then try to link to the resource referenced by the variable node. As far as I can tell, edit_node_path will only require an argument if neither variable exists.

My concern is that this behavior is inconsistent: if the show and destroy links within the partial for a resource call the show and destroy actions for the partial's resource, why should the edit link be any different?

@spastorino
Ruby on Rails member

Not sure if I follow you but edit_node_path requires a node which is the one that the edit will point to.
Please explain a bit more the issue and I can reopen it.

@spastorino spastorino closed this Jul 11, 2011
@jthemphill
@spastorino
Ruby on Rails member

@jthemphill thanks for doing your best to explain the issue :).
Can you provide an app in order to reproduce the problem?.

@jthemphill
@spastorino
Ruby on Rails member

@jthemphill push it to github and send us the link

@jthemphill

Okay, you were right.

I made a sample app, but edit_node_path doesn't work without any arguments in a brand-new app. I'm going to dig through my app to see if it overrides the built-in function somewhere, but the issue is definitely not in Rails. Sorry for the false alarm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment