本文介绍了我可以使用 Rails link_to 来制作 POST 而不是 button_to 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 inline_svg gem 为我的图标渲染 SVG.我的视图代码如下所示:
<%= link_to inline_svg(listing.favorite_icon, class: "svg"), favorite_listing_path(id: listing.id), method: :post %>
我有一个心形图标,当用户点击它时,应该对 Rails favorite
路由进行 POST
.我在 Postman 中对此进行了测试,并且在我的视野之外它确实可以正常工作.但是,在我看来,单击收藏夹会生成 GET
.
我知道我可以使用 button_to
而不是 link_to
然后 POST
就会发生..但是...button_to
无法正确呈现我的 SVG(请参阅附图).
所以我可以选择以下两种方式之一...有人可以帮我将 link_to 设置为 POST
吗?或者有人可以告诉我为什么我的 SVG 无法使用 button_to
正确呈现?
使用link_to
:
使用button_to
:
更新:以下是我尝试过的一些事情...单击仍然会导致 GET
.另外,如果这有什么不同的话,我正在使用 Rails 4.2.
<%= link_to(inline_svg(listing.favorite_icon, class: "svg"), controller: "listings", action: "favorite", id: listing.id, method: :post) %>
<div class="icon"><%= inline_svg(listing.favorite_icon, class: "svg") %>
<%结束%>
解决方案
我想通了.当我开始 Rails 项目时,我去掉了 Rails-Jquery.我没有意识到 { method: POST }
使用这个.
I am using the inline_svg gem to render SVGs for my icons.My view code looks like this:
<div class="icon">
<%= link_to inline_svg(listing.favorite_icon, class: "svg"), favorite_listing_path(id: listing.id), method: :post %>
</div>
I have a heart icon that, when the user clicks it, a POST
should be made to the Rails favorite
route. I tested this in Postman and it does correctly work outside of my view. But, inside of my view, clicking favorite generates a GET
.
I know I can use button_to
instead of link_to
and the POST
will then happen..BUT...button_to
doesn't render my SVG properly (see attached pics).
So I can go one of two ways...can someone help me get my link_to to POST
? Or can someone give me a clue as to why my SVG doesn't render correctly with button_to
?
With link_to
:
With button_to
:
UPDATE: Here are some things I have tried...clicking still results in a GET
. Also, I'm on Rails 4.2 if that makes a difference at all.
<%= link_to "hi", controller: "listings", action: "favorite", id: listing.id, method: :post %>
<%= link_to(inline_svg(listing.favorite_icon, class: "svg"), controller: "listings", action: "favorite", id: listing.id, method: :post) %>
<%= link_to(favorite_listing_path(id: listing.id), method: :post) do %>
<div class="icon">
<%= inline_svg(listing.favorite_icon, class: "svg") %>
</div>
<% end %>
解决方案
I figured this out. When I started the Rails project, I stripped out Rails-Jquery. I didn't realize the { method: POST }
uses this.
这篇关于我可以使用 Rails link_to 来制作 POST 而不是 button_to 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!