简体   繁体   English

Rails路线和控制器动作

[英]Rails routes and controller actions

In my controller I have - 在我的控制器中-

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

... and in my view I have - ...而且我认为-

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

... in routes.rb I simply have - ...在routes.rb中,我只是-

resources :chap_comments 资源:chap_comments

... but before the page with the link in even renders I'm getting No route matches {:action=>"remove_file", :controller=>"chap_comments", :id=>1} - what should I be putting in my routes? ...但在带有链接的页面甚至渲染之前,我得到的No route matches {:action=>"remove_file", :controller=>"chap_comments", :id=>1} -我应该放入什么我的路线?

Try using a member : 尝试使用member

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

This adds an extra resource path for remove_file . 这为remove_file添加了额外的资源路径。 The full path would be remove_file_chap_comment_path . 完整路径为remove_file_chap_comment_path

You need to add route for remove_file in your routes file. 您需要在路由文件中为remove_file添加路由。 like below: 如下所示:

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

Change your routes like 更改路线,例如

resources :chap_comments do
  member do
    delete :remove_file
  end
end

Then you can make a link (run rake routes to see the full path name to method) 然后,您可以建立链接(运行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