显示 "no route matches" 删除 link rails 4

Shows "no route matches" for delete link rails 4

我是 ruby 的新手 rails 4. link 删除 action.the 错误是:

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

即便如此,数据也会从数据库中销毁

但是 link 在我的 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

中的销毁按钮也是如此

现在我卡住了。

destroy 操作的路径与 show 操作的路径相同(当然,如果您使用 resources)。唯一不同的是 HTTP 方法,GET 表示 showDELETE 表示销毁。所以你的 link 应该如下所示:

<%= link_to "Delete", subject, method: :delete %>

此外,您不应该为 subjects 控制器中的各种操作设置冗余路由,例如 get 'subjects/index' 并且您不应该定义成员 show 操作,就像您尝试做(这没有任何意义)。相反,在你的路线中,你应该只有:

resources :subjects

您的某些 link 可能会在此次修改后停止工作,您必须修复它们,以便它们使用正确的路线。

始终供参考..在本地文件中复制路线..使用

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 动词才能工作... 例如,只有当我的方法调用是@Marek Lipka 所说的 delete 类型时,我的删除操作才会起作用....

这个问题很旧,但还没有选择答案,所以我会给我 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 路由有一个 "first appearance priority",如您的日志 [=28] 所示=]路由优先级从上到下匹配,这意味着你正在创建一个GET destroy路由,resources :subjects创建的DELETE destroy被忽略。

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

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

并使用控制器上的 destroy 方法来执行操作。

用户

GET /users(.:format) 用户#index

POST /users(.:format) users#create

new_user GET /users/new(.:format) 用户#new

edit_user GET /users/:id/edit(.:format) 用户#edit

user GET /users/:id(.:format) users#show

PATCH /users/:id(.:format) users#update

PUT /users/:id(.:format) 用户#update

删除 /users/:id(.:format) 用户#destroy

可以直接删除

<%= link_to("Delete", user, method: :delete)%>

where user include id default and it will delete your users by default

如果你想先执行 delete 方法,然后从那里执行 destroy 方法,你需要在你的路由文件中添加以下路由,但它应该在你的应用程序的最后或默认路由。

match ':controller(/:action(/:id))', :via => [:get, :post]