Rails: How to add link_to with target blank
Asked Answered
P

3

84

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...

Parrisch answered 8/8, 2012 at 22:46 Comment(0)
C
161

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

Coadunate answered 8/8, 2012 at 23:3 Comment(5)
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.Parrisch
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.Coadunate
Link_to_blank works like a charm and it did the trick! Thanks very much.Parrisch
See my answer for a method that will also support blocks and passing no parameters.Spacesuit
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.Tachograph
S
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
Spacesuit answered 20/12, 2014 at 5:20 Comment(0)
C
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
Caress answered 21/9, 2022 at 2:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.