Rails: How to add link_to with target blank

后端 未结 2 703
逝去的感伤
逝去的感伤 2021-02-11 12:07

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 b         


        
2条回答
  •  悲&欢浪女
    2021-02-11 12:40

    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

提交回复
热议问题