【发布时间】:2015-03-28 10:01:57
【问题描述】:
我是使用 rails 4 的 ruby 新手。错误出现在删除操作的链接中。错误是:
Subjects#index 中的 ActionController::UrlGenerationError 无路由 匹配 {:action=>"delete", :controller=>"subjects", :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
现在如果我像这样通过 URL 手动调用删除
http://localhost:3000/subjects/delete?id=8
然后模板正在加载。
现在如果我通过像这样的 URL 手动调用销毁
http://localhost:3000/subjects/destroy?id=8
即使这样,数据也会从数据库中销毁
但我的 list.html.erb 中的链接
<%= link_to 'Delete', subject, method: :delete %>
正在调用导致此 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 中的销毁按钮也是如此
现在我被困住了。
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 routes action