Joe Morano
Joe Morano

Reputation: 1895

Can I apply remote: true to an html href link?

I need an entire div to be a link, and the only way I've found to do that is to insert the div into a plain html href link.

But I also need the link to be remote. I made a guess:

<a href="www.link.com" remote=true>
  <div id='link'></div>
</a>

but while this syntax doesn't throw any errors, clicking on the link still opens a new page.

Is it possible to apply the Rails link method remote: true to an html href link?

Upvotes: 2

Views: 1910

Answers (2)

x6iae
x6iae

Reputation: 4164

This is an example of what I call "combining two right ways of doing something in the wrong way".

It's either you use your pure html and write a javasript script to give the div an onclick function, or you use the rubys' link_to and set remote: true: either of this should work fine for you

But combining these two is a no, as the included remote: true will not be interpretable.

<%= link_to "www.link.com", remote: true do %>
  <div></div>
<% end %>

Upvotes: 3

djaszczurowski
djaszczurowski

Reputation: 4515

according to http://guides.rubyonrails.org/working_with_javascript_in_rails.html

you should use one of built in functions to generate remote href <%= link_to "an article", @article, remote: true %> which outputs with a bit different html in compare to yours <a href="/articles/1" data-remote="true">an article</a> (notice remote is a data attribute)

Upvotes: 4

Related Questions