Search code examples
ruby-on-railsnested-routes

Nested routes, passing :user_id


I would like to create New Task button which will redirect to /users/:user_id/tasks/new
Routes:

  devise_for :users
  resources :users do
    resources :tasks, shallow: true
  end

Task Controller:

  def new
    @task = Task.new
  end

View:

<%= link_to "New Task", new_user_task_path(@user) %> 

but it gives me - No route matches {:action=>"new", :controller=>"tasks", :user_id=>nil} missing required keys: [:user_id] error.


Solution

  • Instead of having new_user_task_path(@user) you would like to have probably new_user_task_path(current_user). @user is nil in this case.