【发布时间】:2020-11-28 09:15:30
【问题描述】:
我开始使用 Ruby 并从他们的官方资料中实现简单的博客。
我完成了应用程序,但在破坏操作时遇到了问题。这是表格
<h1>Listing Articles</h1>
<%= link_to 'New article', new_article_path %>
<% if !@articles.nil? %>
<table>
<tr>
<th>Title</th>
<th>Text</th>
<th colspan="3"></th>
</tr>
<% @articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.text %></td>
<td><%= link_to 'Show', article_path(article) %></td>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Destroy', article_path(article),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<% else %>
<p> no avaiable articles </p>
<% end %>
那是可用的路线
Prefix Verb URI Pattern Controller#Action
welcome_index GET /welcome/index(.:format) welcome#index
root GET / welcome#index
article_comments GET /articles/:article_id/comments(.:format) comments#index
POST /articles/:article_id/comments(.:format) comments#create
article_comment GET /articles/:article_id/comments/:id(.:format) comments#show
PATCH /articles/:article_id/comments/:id(.:format) comments#update
PUT /articles/:article_id/comments/:id(.:format) comments#update
DELETE /articles/:article_id/comments/:id(.:format) comments#destroy
articles GET /articles(.:format) articles#index
POST /articles(.:format) articles#create
new_article GET /articles/new(.:format) articles#new
edit_article GET /articles/:id/edit(.:format) articles#edit
article GET /articles/:id(.:format) articles#show
PATCH /articles/:id(.:format) articles#update
PUT /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#destroy
您可能会注意到表单链接上的delete 方法和DELETE 请求映射到articles#destroy 操作。但是,单击此链接时会调用 GET 而不是 DELETE(相同的 URI 模式),但我还没有找到根本原因。
路由本身在下面
Rails.application.routes.draw do
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
get 'welcome/index'
root 'welcome#index'
post '/articles/:id', to: 'articles#update'
resources :articles, only: [:new, :edit, :index, :create, :show, :update, :destroy] do
resources :comments
end
end
我成功地为显式暴露的路由定义别名,但除了引入新问题之外,它没有使用平台原生方式resources :articles
您能否帮助了解\找出此问题的原因?
【问题讨论】:
-
我猜
<td><%= link_to 'Destroy', article, method: :delete, data: { confirm: 'Are you sure?' } %></td>工作?
标签: ruby