无法使用link_to或button_to rails访问嵌套路由
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法使用link_to或button_to rails访问嵌套路由相关的知识,希望对你有一定的参考价值。
我在访问路径时遇到问题,这条路线在我运行路线时似乎没有嵌套,但是嵌套在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}
我很困惑,因为当我耙路线时,它返回有一个列表的创建动作。我显然无法正常访问它。我感谢任何帮助。
答案
试试这个: -
根据你的路线,你需要通过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%>
以上是关于无法使用link_to或button_to rails访问嵌套路由的主要内容,如果未能解决你的问题,请参考以下文章
为啥 button_to 不能使用方法: :put with slim in rails
在 button_to 中使用 :method => delete 的目的是啥?
无法选择 link_to 标记作为 jquery 选择器 Rails
Rails 3 - link_to 使用 jquery ajax 调用部分