我是ruby with rails 4的新手。错误发生在删除操作的链接中。错误是:
主题中的ActionController :: UrlGenerationError #index无路由 匹配{:action =>"删除",:controller =>"科目",:id => 1}
我认为问题出在路由上。 我的list.html.erb代码是:
<div class="subject list">
<h2>Subjects</h2>
<%= link_to("Add New Subject", {:action => 'new'}, :class => 'action new') %>
<table class="listing" summary="Subject list">
<tr>
<th>Position</th>
<th>Subject</th>
<th>Visible</th>
<th>Pages</th>
<th>Actions</th>
</tr>
<% @subjects.each do |subject| %>
<tr>
<td><%= subject.position %></td>
<td><%= subject.name %></td>
<td class="center"><%= subject.visible ? 'Yes' : 'No' %></td>
<td class="center"><%= subject.pages.size %></td>
<td class="actions">
<%= link_to("Show",{:action => 'show', :id => subject.id}, :class => 'action show') %>
<%= link_to("Edit",{:action => 'edit', :id => subject.id}, :class => 'action edit') %>
<!--error is showing in this line--> <%= link_to("Delete",{:action => 'delete', :id => subject.id}) %>
</td>
</tr>
<% end %>
</table>
</div>
路由代码是 -
resources :subjects
列表,显示,编辑,更新的所有操作都在工作,除了删除和销毁,因为我的路由不成熟。 建议会有所帮助。
我更改了路线文件:
get 'subjects/index'
get 'subjects/list'
get 'subjects/new'
get 'subjects/edit'
get 'subjects/delete'
get 'subjects/destroy'
resources :subjects do
member do
get 'show', to: 'show#id'
end
end
现在如果我通过这样的网址手动调用删除
http://localhost:3000/subjects/delete?id=8
然后模板正在加载。
现在如果我通过URL手动调用destroy,如
http://localhost:3000/subjects/destroy?id=8
即使这样,数据也会从数据库中销毁
但是我的list.html.erb中的链接
<%= link_to 'Delete', subject, method: :delete %>
正在调用show action导致此url
http://localhost:3000/subjects/1/show
and the error occurs :
No route matches [DELETE] "/subjects/1/show"
Rails.root: D:/ruby/sam_app
Application Trace | Framework Trace | Full Trace
Routes
Routes match in priority from top to bottom
Helper HTTP Verb Path Controller#Action
Path / Url
subjects_index_path GET /subjects/index(.:format) subjects#index
subjects_list_path GET /subjects/list(.:format) subjects#list
subjects_new_path GET /subjects/new(.:format) subjects#new
subjects_edit_path GET /subjects/edit(.:format) subjects#edit
subjects_delete_path GET /subjects/delete(.:format) subjects#delete
subjects_destroy_path GET /subjects/destroy(.:format) subjects#destroy
subject_path GET /subjects/:id/show(.:format) show#id
subjects_path GET /subjects(.:format) subjects#index
POST /subjects(.:format) subjects#create
new_subject_path GET /subjects/new(.:format) subjects#new
edit_subject_path GET /subjects/:id/edit(.:format) subjects#edit
GET /subjects/:id(.:format) subjects#show
PATCH /subjects/:id(.:format) subjects#update
PUT /subjects/:id(.:format) subjects#update
DELETE /subjects/:id(.:format) subjects#destroy
同样适用于delete.html.erb中的destroy按钮
现在我被卡住了。
答案 0 :(得分:3)
总是在本地file..using
中引用.copy路由 rake routes >> paths.txt
然后你随时可以推荐它......看起来像这样
dashboard_index GET /dashboard(.:format) dashboard#index
POST /dashboard(.:format) dashboard#create
new_dashboard GET /dashboard/new(.:format) dashboard#new
edit_dashboard GET /dashboard/:id/edit(.:format) dashboard#edit
dashboard GET /dashboard/:id(.:format) dashboard#show
PUT /dashboard/:id(.:format) dashboard#update
DELETE /dashboard/:id(.:format) dashboard#destroy
因此,您可以清楚地看到 http动词需要什么方法 ...
例如,我的删除操作只有在我的方法调用类型为delete
时才有效,因为@MarekLipka说....
答案 1 :(得分:3)
这个问题很陈旧,但还没有选择答案,所以我会给出2美分。
此:
get 'subjects/index'
get 'subjects/list'
get 'subjects/new'
get 'subjects/edit'
get 'subjects/delete'
get 'subjects/destroy'
resources :subjects do
member do
get 'show', to: 'show#id'
end
end
将使用GET request
(如果您在浏览器的地址栏中键入它),因为rails路由具有“首次出现优先级”,如日志所示路由匹配优先级从顶部到底部,这意味着您正在创建GET destroy
路由,DELETE destroy
创建的resources :subjects
将被忽略。
我建议你摆脱那些get 'subjects/*'
并使用以下代码作为删除按钮:
<%= link_to 'Delete', subject_path(subject), method: :delete %>
并使用控制器上的destroy
方法执行操作。
答案 2 :(得分:2)
destroy
操作的路径与show
操作相同(当然,如果您使用resources
)。唯一不同的是HTTP方法,GET
show
和DELETE
用于销毁。因此,您的链接应如下所示:
<%= link_to "Delete", subject, method: :delete %>
此外,您不应该在subjects
控制器中为各种操作设置冗余路由,例如get 'subjects/index'
,并且您不应该定义成员show
操作,就像您一样试图这样做(它没有任何意义)。相反,在您的路线中,您应该只有:
resources :subjects
在修改后,您的某些链接可能会停止工作,您必须修复它们才能使用正确的路由。
答案 3 :(得分:0)
用户
获取/users(.:format)users#index
POST /users(.:format)users#create
new_user GET /users/new(.:format)users#new
edit_user GET /users/:id/edit(.:format)users#edit
用户GET /users/:id(.:format)users#show
PATCH /users/:id(.:format)users#update
PUT /users/:id(.:format)users#update
删除/users/:id(.:format)users#destroy
您可以直接删除
<%= link_to("Delete", user, method: :delete)%>
默认情况下,用户包含ID,它将直接删除您的用户
如果您要先执行delete方法,然后从那里执行destroy方法,则需要在路由文件中添加以下路由,但该路由应位于应用程序的最后或默认路由。
match ':controller(/:action(/:id))', :via => [:get, :post]