78

I am new to rails 3, I would like to add (:target => "_blank") to link_to helper below

link_to "GOOGLE", 'http://www.google.com', class: "btn btn-large btn-primary"

But I would like to use the application_helper to define link_to method.

  1. How do I define the link_to methd in application_helper?
  2. How do I pass the class: "btn btn-large btn-primary" in the link_to method?

Thank you for your assistance...

Promise Preston
  • 24,334
  • 12
  • 145
  • 143
joe
  • 946
  • 1
  • 7
  • 14

3 Answers3

157

Why would you want to override link_to? It's already defined in Rails, just use it like this :

link_to "GOOGLE", "http://www.google.com", target: "_blank", class: "btn btn-large btn-primary"

Edit: OK, understood. I'd advise against overriding such a common method so create another one :

def link_to_blank(body, url_options = {}, html_options = {})
  link_to(body, url_options, html_options.merge(target: "_blank"))
end

It should do the trick

Anthony Alberto
  • 10,325
  • 3
  • 34
  • 38
  • Thanks for your quick reply. I did not want to repeat myself (DRY), by placing :target => "_blank" in every link_to helper.. so I was thinking to merge the :target => "_blank" with link_to helper by creating a method in application_helper. But I'm having difficulty to pass the class: "btn btn-large btn-primary" to the new link_to method in application_helper. – joe Aug 08 '12 at 23:22
  • Ok edited my answer, take a look. Use `link_to_blank` instead, I would advise against overriding `link_to` since it's a very widely used method. – Anthony Alberto Aug 09 '12 at 00:48
  • Link_to_blank works like a charm and it did the trick! Thanks very much. – joe Aug 09 '12 at 01:58
  • See my answer for a method that will also support blocks and passing no parameters. – Benjamin Sullivan Dec 20 '14 at 05:22
  • Adding a property/attribute/argument to a function call in multiple places isnt exactly violating the DRY principle. Sometimes people obsess over programming concepts to the point of overkill (which I would call the `link_to_blank` function above). This sort of excessive application of a concept is the beginning of cargo cult programming. – Todd Sep 16 '16 at 16:50
5

Adding to Anthony's answer, this more closely resembles Rails' link_to implementation, including support for blocks and passing no parameters:

def link_to_blank(name = nil, options = nil, html_options = nil, &block)
  target_blank = {target: "_blank"}
  if block_given?
    options ||= {}
    options = options.merge(target_blank)
  else
    html_options ||= {}
    html_options = html_options.merge(target_blank)
  end
  link_to(name, options, html_options, &block)
end
Benjamin Sullivan
  • 1,466
  • 1
  • 12
  • 15
0

Up to now for Rails 7, I suggest a more elegant way according to Rails's implementation of link_to:

def link_to(name = nil, options = nil, html_options = nil, &block)
  html_options, options, name = options, name, block if block_given? 
  link_to(name, options, (html_options || {}).merge(target: '_blank'))
end
Aidi Stan
  • 71
  • 5