Search code examples
ruby-on-railsrouteslink-to

Trouble accessing nested route with link_to or button_to rails


I am having trouble accessing a route which appears to be not nested when I run routes, but is nested in routes.rb

        Prefix Verb   URI Pattern                               Controller#Action
              sort_list POST   /lists/:id/sort(.:format)                 lists#sort
                    list POST   /lists/:id(.:format)                      lists#create
              list_items GET    /lists/:list_id/items(.:format)           items#index
                        POST   /lists/:list_id/items(.:format)           items#create
          new_list_item GET    /lists/:list_id/items/new(.:format)       items#new
              edit_item GET    /items/:id/edit(.:format)                 items#edit
                    item GET    /items/:id(.:format)                      items#show
                        PATCH  /items/:id(.:format)                      items#update
                        PUT    /items/:id(.:format)                      items#update
                        DELETE /items/:id(.:format)                      items#destroy
          project_lists GET    /projects/:project_id/lists(.:format)     lists#index
                        POST   /projects/:project_id/lists(.:format)     lists#create
        new_project_list GET    /projects/:project_id/lists/new(.:format) lists#new
              edit_list GET    /lists/:id/edit(.:format)                 lists#edit
                        GET    /lists/:id(.:format)                      lists#show
                        PATCH  /lists/:id(.:format)                      lists#update
                        PUT    /lists/:id(.:format)                      lists#update
                        DELETE /lists/:id(.:format)                      lists#destroy
                projects GET    /projects(.:format)                       projects#index
                        POST   /projects(.:format)                       projects#create
            new_project GET    /projects/new(.:format)                   projects#new
            edit_project GET    /projects/:id/edit(.:format)              projects#edit
                project GET    /projects/:id(.:format)                   projects#show
                        PATCH  /projects/:id(.:format)                   projects#update
                        PUT    /projects/:id(.:format)                   projects#update
                        DELETE /projects/:id(.:format)                   projects#destroy
                  react POST   /react(.:format)                          reacts#create
              new_react GET    /react/new(.:format)                      reacts#new
              edit_react GET    /react/edit(.:format)                     reacts#edit
                        GET    /react(.:format)                          reacts#show
                        PATCH  /react(.:format)                          reacts#update
                        PUT    /react(.:format)                          reacts#update
                        DELETE /react(.:format)                          reacts#destroy

Here is Routes.rb

Rails.application.routes.draw do
  resources :projects do
    resources :lists, shallow: true do
      member do
        post :sort
      end
      resources :items, shallow: true
    end
  end
  resource :react
end

And my link_to

  <%= link_to %(<span class="glyphicon glyphicon-trash"></span>).html_safe, controller: :lists, action: :create ,remote: true, method: :post %>

I get the error

No route matches {:action=>"create", :controller=>"lists", :method=>:post}

I am confused because when I rake routes it returns that there is a create action for list. I obviously can't access it normally. I appreciate any help.


Solution

  • try this: -

    As per your routes you need to pass project_id for create action as params

    <%= link_to project_lists_path(project_id: project.id), method: :post, remote: true do%>
      <span class="glyphicon glyphicon-trash"></span>
    <%end%>
    

    or you can also try this:-

    <%= link_to project_lists_path(project), method: :post, remote: true do%>
      <span class="glyphicon glyphicon-trash"></span>
    <%end%>