gpt4 book ai didi

ruby-on-rails - 显示 "no route matches"用于删除链接 rails 4

转载 作者:行者123 更新时间:2023-12-04 06:14:44 28 4
gpt4 key购买 nike

我是使用 rails 4 的 ruby​​ 新手。错误出现在删除操作的链接中。错误是:

ActionController::UrlGenerationError in Subjects#index No route matches {: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 中的销毁按钮也是如此

现在我被困住了。

最佳答案

这个问题很老,但还没有选择的答案,所以我会给我 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 请求(如果您在浏览器的地址栏上键入它),因为 rails 路由具有“首次出现优先级”,如您的日志中所示 Routes match优先级从上到下,这意味着您正在创建一个 GET destroy 路由和 resources :subjects 创建的 DELETE destroy 是忽略。

我建议您摆脱那些 get 'subjects/*' 并使用以下代码作为您的删除按钮:

<%= link_to 'Delete', subject_path(subject), method: :delete %>

并使用 Controller 上的 destroy 方法来执行您的操作。

关于ruby-on-rails - 显示 "no route matches"用于删除链接 rails 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28210296/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com