Rails: using link_to to make a link without href

<%= 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')
    }
  });
}

I use this approach:

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

Its basically a Javascript no-op


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


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.