简体   繁体   中英

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 -

resources :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?

Try using a member :

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

This adds an extra resource path for remove_file . The full path would be remove_file_chap_comment_path .

You need to add route for remove_file in your routes 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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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