Routing Error - No route matches when using button_to with custom action

后端 未结 3 2025
礼貌的吻别
礼貌的吻别 2021-01-23 11:34

I have the following button to download a file.

= button_to \'download\', action: \'download\', method: \'get\'

And I have a download

相关标签:
3条回答
  • 2021-01-23 11:38

    Try this, it should work with your view code just change into routes.rb

    resources :movies do
        get :download, :on => :collection
    end
    
    0 讨论(0)
  • 2021-01-23 11:46

    In your routes.rb:

    resources :movies do
      get 'download', on: :member
    end
    

    Now, in your view you need to specify, what movie you want to download:

    = button_to 'download', download_movie_path(@movie), method: 'get'
    

    Also, note:

    1. Use path instead of specifying controller/action in helpers.

    2. Use link_to for GET requests and if you need link with button style apply it through CSS. button_to with GET request is a bad practice.

    0 讨论(0)
  • 2021-01-23 11:58

    Alternatively, if you want to specify controller/action (which has advantages, since it allows you to pass through arbitrary params), you'll need to also explicitly pass along any parameters that action relies on (assuming you're trying to download an individual movie, and not the entire collection).

    button_to 'download', {controller: 'movies', action: 'download', id: movie.id }, method: 'get'
    

    Also, ditto Mikhail D's point about using link_to for "get" requests. Defining the method explicitly is great for sending requests to the "update" action (by setting method: :patch or method: :puts), but for "gets" just use link_to.

    0 讨论(0)
提交回复
热议问题