1

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:

Before

With button_to:

After

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 %>
Kelly
  • 619
  • 8
  • 18
  • 1
    I don't know if this will help or not but try this: `<%= link_to(inline_svg(listing.favorite_icon, class: "svg"), favorite_listing_path(id: listing.id), method: :post) %>` or may be you can use a `link_to` block using the `do end`. – Deepesh Sep 07 '15 at 05:22
  • @Deep Point them to the [documentation](http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to) and you could convert that comment into a decent answer. – mu is too short Sep 07 '15 at 05:24
  • As far as the `button_to` stuff goes, you probably want to throw in a `.html_safe` call to tell Rails that your SVG string doesn't need to be HTML-escaped. – mu is too short Sep 07 '15 at 05:26
  • Possible duplicate of http://stackoverflow.com/questions/13414663/using-rails-link-to-for-links-that-post – Brad Werth Sep 07 '15 at 05:36
  • I think best way is to use link_to, and apply button css on it. – jon snow Sep 07 '15 at 05:36
  • @muistooshort Actually I am not sure so I have just posted the comment. Kelly you can refer to the document link provided in the second comment. Or this is another one: http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to – Deepesh Sep 07 '15 at 05:37
  • for verbs :post, :delete, :patch, and :put., if the user has JavaScript disabled, the request will fall back to using GET. So add one more option. `remote: true ` – jon snow Sep 07 '15 at 05:44
  • @Deep, tried both of your suggestions. Neither worked. :( – Kelly Sep 07 '15 at 15:38
  • @Brad Werth, I saw that SO post (and many others as well). I even copied the exact line (so abandoning the SVG) and changed just the path, but my server does a `GET`. Could it be that I set up the path incorrectly? Although, trying it with Postman works. Also I'm on Rails 4. Does that make a difference? – Kelly Sep 07 '15 at 15:41

3 Answers3

1

I figured this out. When I started the Rails project, I stripped out Rails-Jquery. I didn't realize the { method: POST } uses this.

Kelly
  • 619
  • 8
  • 18
1

This post is pretty old, but I wanted to comment with what ended up working for me after coming across it.

<%= link_to story_favourite_path(@story), method: :post do %>
    <%= render inline: Rails.root.join('public/bookmark-regular.svg').read %>
<% end %>
sandypockets
  • 350
  • 3
  • 13
0

For verbs :post, :delete, :patch, and :put., if the user has JavaScript disabled, the request will fall back to using GET. So add one more option. remote: true to over come from your first issue.

<div class="icon">
  <%= link_to inline_svg(listing.favorite_icon, class: "svg"), favorite_listing_path(id: listing.id), method: :post, remote: true  %>
</div>
jon snow
  • 3,062
  • 1
  • 19
  • 31
  • Thanks for the help. This didn't work. It's still doing a `GET`. Also, I tried wrapping everything after the link_to in parens but no luck. – Kelly Sep 07 '15 at 15:26