繁体   English   中英

Rails路线和控制器动作

[英]Rails routes and controller actions

在我的控制器中-

def remove_file
  @chap_comment.remove_chap_comment_pdf!
  @chap_comment.save
end

...而且我认为-

<%= link_to 'remove pdf', controller: 'chap_comments', action: 'remove_file', id: comment.id %>

...在routes.rb中,我只是-

资源:chap_comments

...但在带有链接的页面甚至渲染之前,我得到的No route matches {:action=>"remove_file", :controller=>"chap_comments", :id=>1} -我应该放入什么我的路线?

尝试使用member

  resources :chap_comments do
    member do
      delete 'remove_file'
    end
  end

这为remove_file添加了额外的资源路径。 完整路径为remove_file_chap_comment_path

您需要在路由文件中为remove_file添加路由。 如下所示:

get "chap_comments/:id/remove_file" => 'chap_comments#remove_file'

更改路线,例如

resources :chap_comments do
  member do
    delete :remove_file
  end
end

然后,您可以建立链接(运行rake routes以查看方法的完整路径名)

= link_to "Remove PDF", remove_file_chap_comment_path(id), :method => :delete

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM