0

I want to write a todo list app in rails4, and the todo list app will look like

1.task one    
  *todo 1    
  *todo 2    
2.task two    
  *todo 1    
  *todo 2

So I want to demo all the things in the task's index, which also fetch their tasks' todos separately. Now, my url will look like project/1/tasks, and this will show all the things I need.

I don't know how to do it. I have two tables: tasks and todos. So, currently I have a thought, which renders 'todos/index' in 'tasks/index', so I need to pass a parameter task_id in order to fetch the todos separately.

How do I get parameter task_id in todos_controller? And I will not access it by url, I will render it in task's index view.

1 Answer 1

0

You should use nested resources:

http://guides.rubyonrails.org/routing.html#nested-resources

For example:

# routes.rb
resources :tasks do
  resources :todos
end

And in your todos controller

# todos_controller.rb
def index
  @task = Task.find(params[:task_id])
  @todos = @task.todos
  # ...
end

def show
  @task = Task.find(params[:task_id])
  @todos = @task.todos.find(params[:id])
  # ...
end
1
  • here is the problem I already use the nested resources resources :projects do resources :tasks end And I don't want to use a three-level nested resources. May 22, 2014 at 5:44

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.