43

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.

bevanb
  • 8,201
  • 10
  • 53
  • 90

8 Answers8

50

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

niels
  • 1,719
  • 10
  • 20
41

I use this approach:

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

Its basically a Javascript no-op

Cody Caughlan
  • 32,456
  • 5
  • 63
  • 68
  • 2
    This works. Ended up just making an `a`-tag without an href- that way browsers won't show the `javascript:;` link destination preview in the lower-left. – bevanb Aug 22 '12 at 21:59
  • 3
    What's the difference from `javascript:void(0)`? – freemanoid Sep 23 '14 at 15:31
13

Set the href to:

javascript:void(0)

This will let you have a link that clicking it does nothing, doesn't reload the page, and doesn't move the page up to the top.

Solomon
  • 6,145
  • 3
  • 25
  • 34
3
<%= link_to '+ Add Make', 'javascript:void(0)', id: 'add-make', onclick: 'addMake()' %>

and then your jQuery:

function addMake() {
  $.ajax({
    url: '/dealers',
    type: 'GET',
    dataType: 'script',
    success: function (data) {
      console.log('it was successful')
      $('.add-make').hide();
    },
    error: function (data) {
      console.log('it was error')
    }
  });
}
Kos
  • 4,890
  • 9
  • 38
  • 42
Andrey
  • 191
  • 2
  • 6
2

An alternative is

= link_to("My hyperlink", "#")

This will give you a link that when clicked, does not reload the page like passing in nil. At the same time, it will set the mouse to pointer on mouseover, which you will not get with the content_tag approach.

Aaron Gray
  • 11,283
  • 7
  • 55
  • 61
  • 1
    This is okay in some places, but it will bounce the screen to the top of your page. Adding the 'javascript:;' as suggested above probably the better way to go in most cases. – jacklin Aug 14 '14 at 02:36
2

I think you're better off using a button in situations where there is no href.

<button class='button'>Hello</button>
stephenmurdoch
  • 34,024
  • 29
  • 114
  • 189
1

An example for rails 5 would be:

<%= content_tag("a", image_tag("/logo.png"), class: "navbar-brand") %>

This would render as:

<a class="navbar-brand"><img src="/logo.png" alt="Logo"></a>

This is similar to niels' answer but with a nested image_tag and a class.

Source: Rails API

Community
  • 1
  • 1
tordoch
  • 11
  • 3
0

I prefer using plain HTML without href attribute at all in such cases:

<a data-toggle="modal" data-target="#myModal">Open dialog</a>

IMHO it is more readable than content_tag("a","link text").

  • The disadvantage of href=# is that the browser will scroll to the top when clicked by default
  • javascript:; looks ugly
Artur INTECH
  • 6,024
  • 2
  • 37
  • 34