无法使用link_to或button_to rails访问嵌套路由

时间:2017-12-28 03:37:36

标签: ruby-on-rails routes link-to

我无法访问在运行路线时似乎没有嵌套的路线,但是它嵌套在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

这是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

我的link_to

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

我收到错误

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

我很困惑,因为当我搜索路线时,它会返回列表的创建操作。我显然无法正常访问它。我感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

试试这个: -

根据您的路线,您需要将project_id作为params

传递给创建操作
<%= link_to project_lists_path(project_id: project.id), method: :post, remote: true do%>
  <span class="glyphicon glyphicon-trash"></span>
<%end%>

或者你也可以试试这个: -

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