Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: using link_to to make a link without href

How can I use the link_to helper to make a link without an href attribute? I want to make a link exclusively for a javascript action, I don't want it reloading the page or anything.

When I omit the url parameter, clicking the link reloads the page. Ditto for providing nil in place of the url.

like image 968
bevanb Avatar asked Aug 22 '12 20:08

bevanb


People also ask

How do I create a link in rails?

The rails link_to is a helper which helps us in creating a hyperlink to the existing main document. The Rain link_to will be created as anchor tag or the h-ref tag. Anchor tag is known as an HTML code that creates another link to another page or to a specific section of the existing document.

How does Link_to work in Rails?

Rails is able to map any object that you pass into link_to by its model. It actually has nothing to do with the view that you use it in. It knows that an instance of the Profile class should map to the /profiles/:id path when generating a URL.

How do I link pages in Ruby on Rails?

open index. html. erb file in app>>views>>home folder use in atom editor. After you open index file now add following HTML link code in that file and end with <br /> tag.

What is Link_to?

In computing, a hyperlink, or simply a link, is a reference to data that the user can follow by clicking or tapping. A hyperlink points to a whole document or to a specific element within a document. Hypertext is text with hyperlinks.


2 Answers

You can also use a

content_tag("a","link text") 

which gives you

<a>link text</a> 

or, from the comment from @cesartalves, with a block:

content_tag("a") do   image_tag("src")  end 

See: rails api

like image 124
niels Avatar answered Sep 20 '22 15:09

niels


I use this approach:

= link_to('Click me', 'javascript:;', :id => :foo) 

Its basically a Javascript no-op

like image 38
Cody Caughlan Avatar answered Sep 20 '22 15:09

Cody Caughlan