路由错误 - 将 button_to 与自定义操作一起使用时没有路由匹配 [英] Routing Error - No route matches when using button_to with custom action

查看:44
本文介绍了路由错误 - 将 button_to 与自定义操作一起使用时没有路由匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下按钮可以下载文件.

I have the following button to download a file.

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

我在 MoviesController 下有一个 download 方法

And I have a download method under MoviesController like this

# movies_controller.rb
def download
  …
end

但是,我收到以下错误.知道如何解决这个问题吗?

However, I get the following error. Any idea how to solve this?

Routing Error

No route matches {:action=>"download", :method=>"get", :controller=>"movies"}

推荐答案

在你的 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'

另外,请注意:

  1. 使用 path 而不是在 helpers 中指定 controller/action.

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

link_to 用于 GET 请求,如果您需要按钮样式的链接,请通过 CSS 应用它.button_to 使用 GET 请求是一种不好的做法.

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.

这篇关于路由错误 - 将 button_to 与自定义操作一起使用时没有路由匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆