没有路由匹配 [PUT] 但我在 routes.rb 中包含了 "resources"

No route matches [PUT] but I included "resources" in routes.rb

我正在编写一个包含两种类型文章的博客:"drafts" 和 "published"。我正在使用 aasm gem 使文章从草稿过渡到已发布或 viceverza。还有三种类型的用户:"regular readers"、"editors"和"admins"。

当用户写文章时,管理员可以评估是否发布它们。为实现这一点,管理员有一个视图,他们可以在其中查看草稿和已发布的文章。

问题是,当我尝试发布文章时,出现 No route matches [PUT] "/articles" 错误,无论我添加了 resources :articlesroutes.rb.

我写的代码如下:

routes.rb

resources :categories
resources :articles do
   resources :comments, only: [:create, :update, :destroy, :show]
end 
devise_for :users
root 'welcome#index'
get '/dashboard', to: 'welcome#dashboard'
put '/articles/:id/publish', to: 'articles#publish'

articles_controller.rb

...
def publish
     @article.publish! # Article transition from draft to published.
     redirect_to @article
end
...

dashboard.html.erb

...
<% @articles.each do |art| %>
<h1><%= link_to art.title, art, method: :get %> | id = <%= art.id %> | user_id = <%= art.user_id %></h1>
<div>
    <%= art.body %> - <%= link_to "Eliminar", art, method: :delete %>
    <% if art.may_publish? %>
        - <%= link_to "Publicar", '/articles/#{article.id}/publish' , method: :put %>
    <% end %>
</div>
<% end %>
...

如果包含文章资源,我不明白为什么会出现此错误。如果您需要我包含更多代码,请随时询问我。

在此先致谢。

您应该删除您的自定义路由并将其放入资源中:像这样的文章: routes.rb

resources :articles do
  put 'publish', on: :member
  resources :comments, only: [:create, :update, :destroy, :show]
end

你应该在你的视图中使用它:

<%= button_to "Publicar", publish_article_path(article), method: :put %>

它将生成一个表格。

代码似乎是正确的,所以尝试重置服务器,顺便重启浏览器。它通常会解决像这样的奇怪问题 :) 让我知道它是否有效。