简体   繁体   English

Ruby on Rails:link_to 与 button_to

[英]Ruby on Rails: link_to vs button_to

I am having trouble implementing the method link_to.我在实现方法link_to 时遇到了问题。 My program uses a link to call a controller method which performs some logic and renders a partial.我的程序使用一个链接来调用一个控制器方法,该方法执行一些逻辑并呈现部分。 The issue is I cant get the link to call the controller method.问题是我无法获得调用控制器方法的链接。 I have tried calling the same controller method with button_to and it seems to work.我尝试使用 button_to 调用相同的控制器方法,它似乎有效。 Looking at the API of both ActionView helper methods button_to and link_to it seems like the parameters should be the same.查看 ActionView 辅助方法 button_to 和 link_to 的 API,参数似乎应该相同。 However, the following two lines of code do two different things.但是,以下两行代码做了两件不同的事情。

_product.html.erb _product.html.erb

<%= link_to 'view individual', products_view_individual_product_page_path(product_id: product.id)%>
<%= button_to 'view individual', products_view_individual_product_page_path(product_id: product.id)%>

The link_to call will cause an error ActiveRecord::RecordNotFound error stating "Couldn't find Product with 'id'=view_individual_product_page" and the passed parameters are {"product_id"=>"261", "id"=>"view_individual_product_page"}, im not sure why it is trying to pass the path of my controller method as the id. link_to 调用将导致错误 ActiveRecord::RecordNotFound 错误,指出“无法找到带有 'id'=view_individual_product_page 的产品”,传递的参数为 {"product_id"=>"261", "id"=>"view_individual_product_page"} ,我不知道为什么它试图将我的控制器方法的路径作为 id 传递。 However the button_to line of code works just fine, and I am in the specified controller method.但是 button_to 代码行工作得很好,我在指定的控制器方法中。 Here is the route entry in my routes.rb file...这是我的 routes.rb 文件中的路由条目...

routes.rb路由文件

post 'products/view_individual_product_page'

Can anyone explain to me what I am doing wrong here, I cant find much information online about why the path is being passed as an ID in link_to but not button_to任何人都可以向我解释我在这里做错了什么,我在网上找不到很多关于为什么将路径作为 ID 传递到 link_to 而不是 button_to 的信息

Add method: :post as an option for link_to.添加method: :post作为link_to 的一个选项。

link_to 'view individual', products_view_individual_product_page_path(product_id: product.id), method: :post

"A" tags always does GET requests and you are expecting a POST request. “A”标签总是执行 GET 请求,而您期待 POST 请求。 Note that you need Rails Unobtrusive Javascript for links to trigger a POST request with Rails doing some magic behind the scenes (check your javascript application.js, you should have a require rails-ujs ).请注意,您需要 Rails Unobtrusive Javascript 链接来触发 POST 请求,Rails 在幕后做一些魔术(检查您的 javascript application.js,您应该有一个require rails-ujs )。

Usually, if you need to do a POST request it's okey to have a button_to since it actually renders a FORM tag with a button to do a proper POST request without the need of Rails UJS.通常,如果您需要执行 POST 请求,那么使用button_to就可以了,因为它实际上会呈现带有按钮的 FORM 标记来执行正确的 POST 请求,而无需 Rails UJS。

Check the doc for both: https://api.rubyonrails.org/v5.2.3/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to https://api.rubyonrails.org/v5.2.3/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to检查两者的文档: https : //api.rubyonrails.org/v5.2.3/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to https://api.rubyonrails.org/v5.2.3/classes /ActionView/Helpers/UrlHelper.html#method-i-link_to

I think the difference is that link_to by default use a GET method, where button_to use POST .我认为不同之处在于link_to默认使用GET方法,而button_to使用POST So if you want a different method, specify it因此,如果您想要不同的方法,请指定它

<%= link_to 'view individual', products_view_individual_product_page_path(product_id: product.id), method: :post %>

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

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