Ruby on Rails:link_to与button_to [英] Ruby on Rails: link_to vs button_to

查看:71
本文介绍了Ruby on Rails:link_to与button_to的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在实现方法link_to时遇到麻烦.我的程序使用链接来调用控制器方法,该方法执行一些逻辑并渲染部分逻辑.问题是我无法获得调用控制器方法的链接.我尝试用button_to调用相同的控制器方法,它似乎可以工作.看一下ActionView辅助方法button_to和link_to的API,似乎参数应该相同.但是,以下两行代码可以做两种不同的事情.

_product.html.erb

 <%=链接到查看个人",products_view_individual_product_page_path(product_id:product.id)%><%= button_to'查看个人',products_view_individual_product_page_path(product_id:product.id)%> 

link_to调用将导致错误ActiveRecord :: RecordNotFound错误,指出找不到具有'id'= view_individual_product_page'的产品,并且传递的参数为{"product_id" =>"261","id" =>"view_individual_product_page},我不确定为什么要尝试将控制器方法的路径作为id传递.但是,button_to代码行工作得很好,并且我在指定的控制器方法中.这是我的routes.rb文件中的路由条目...

routes.rb

 发布产品/view_individual_product_page" 

任何人都可以向我解释我在这里做错了什么,我在网上找不到太多有关为什么在link_to而不是button_to中将路径作为ID传递的信息

解决方案

添加方法::post 作为link_to的选项.

 链接到查看个人",products_view_individual_product_page_path(product_id:product.id),方法::post 

"A"表示标签始终会执行GET请求,并且您期望POST请求.请注意,您需要使用Rails Unobtrusive Javascript来触发POST请求,而Rails会在后台做一些魔术(检查javascript application.js,您应该有一个 require rails-ujs ).

通常,如果您需要执行POST请求,那么拥有一个 button_to 很好,因为它实际上呈现了带有按钮的FORM标签来执行正确的POST请求,而无需使用Rails UJS./p>

检查两个文档: 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 am having trouble implementing the method 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. Looking at the API of both ActionView helper methods button_to and link_to it seems like the parameters should be the same. However, the following two lines of code do two different things.

_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. However the button_to line of code works just fine, and I am in the specified controller method. Here is the route entry in my routes.rb file...

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

解决方案

Add method: :post as an option for 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. 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).

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.

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

这篇关于Ruby on Rails:link_to与button_to的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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