2

I am having trouble with some Ruby on Rails code. I have a data structure of nested resources (List has_many Tasks and a Task belongs_to a List). On my List.show page, I have the following line of code:

<td><%= link_to 'Show Task', list_tasks_path(@list, task) %></td>

The idea is that this page will link to a "show" page where I can view the details of an individual Task. I want the url to basically be

/lists/:list_id/tasks/:task_id

The problem is that the above code is directing me to

/lists/:list_id/tasks.task_id

on the index page of Task. How can I tell Rails to send me to the show page instead?

I have tried adding :action => show and :controller => tasks, as well as using show_list_tasks_path(@list, task). I also know that the structure itself is there because my show page works fine if I manually enter /lists/:list_id/tasks/:task_id.

Any help is appreciated. Thank you!

-Sum

1
  • what do your routes look like? what does 'list_task_path' do (instead of 'list_tasks_path')? Commented Dec 10, 2011 at 23:05

2 Answers 2

2

The problem here is that you're using list_tasks_path when you should be using list_task_path.

The list_tasks_path method will generate a URL to the index action, as you know already, and will make the second argument be the format for this request.

For more information please read the Routing Guide.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the help! Also, thanks for the reference web site.
The output of rake routes is extremely useful.
2

A simpler way to specify this url would be

<%= link_to 'Show Task', [@list, task] %>

You can also do list_task_path(@list, task) (notice task instead of tasks).

Run rake routes to see exact names rails generates for your routes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.