我是第一次使用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 %>
调用show操作会导致生成此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中的销毁按钮。
现在我卡住了。