我正在构建一个博客文章版本控制系统,用户可以在其中执行以下操作:
帖子的状态设置在帖子的一栏中,状态可以是“草稿”、“事件”、“存档”
现在,每当我点击“发布”或“重新发布”时,它都会将页面标记为“已存档”。
帖子 Controller .rb
def mark_published
@post = Post.find(params[:id])
@post.update(status: 'active')
@post.save
redirect_to confirmation_admin_post_path(@post.id), notice: 'post has been Published.'
end
def mark_archived
@post = Post.find(params[:id])
@post.status = 'archived'
@post.save
redirect_to confirmation_admin_post_path(id: @post.id), notice: 'post has been archived.'
end
show.html.slim
.col-sm-6.col-md-2.col-lg-2
= link_to 'PUBLISH ', mark_published_admin_post_path(@post), class: 'btn btn-success btn-block mb15 btn-lg '
.col-sm-6.col-md-2.col-lg-2
= link_to 'ARCHIVE ', mark_archived_admin_post_path(@post), class: 'btn btn-success btn-block mb15 btn-lg
'
路由文件
namespace :admin do
resources :posts do
member do
get 'posts', to: 'posts#mark_archived', as: :mark_archived
get 'posts', to: 'posts#mark_published', as: :mark_published
get 'posts', to: 'posts#confirmation', as: :confirmation
end
end
end
更新:
mark_archived_admin_post GET /admin/posts/:id/posts(.:format) admin/posts#mark_archived
mark_published_admin_post GET /admin/posts/:id/posts(.:format) admin/posts#mark_published
mark_archived_admin_post GET /admin/posts/:id/posts(.:format) admin/posts#mark_archived
confirmation_admin_post GET /admin/posts/:id/posts/confirmation(.:format) admin/posts#confirmation
最佳答案
作为 rake routes
的输出显示,您的 routes.rb
文件似乎不正确。通过这种方式,您将创建 3 条具有不同名称的路由,并转到不同的 Controller 操作,但路径相同,即 GET 到/admin/posts/:id/posts。尝试将您的文件更改为以下内容:
namespace :admin do
resources :posts do
member do
post 'mark_archived', to: 'posts#mark_archived', as: :mark_archived
post 'mark_published', to: 'posts#mark_published', as: :mark_published
post 'confirmation', to: 'posts#confirmation', as: :confirmation
end
end
end
post
对比 get
没有那么重要。重要的是它们不是全部get 'posts'
.
关于ruby-on-rails - Rails 使用错误的自定义命名路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32016205/