joe*_*joe 53 ruby-on-rails-3.1
我是rails 3的新手,我想在下面的link_to helper中添加(:target =>"_ blank")
link_to "GOOGLE", 'http://www.google.com', class: "btn btn-large btn-primary"
Run Code Online (Sandbox Code Playgroud)
但我想使用application_helper来定义link_to方法.
谢谢您的帮助...
Ant*_*rto 121
你为什么要覆盖link_to?它已经在Rails中定义,只需使用它:
link_to "GOOGLE", "http://www.google.com", target: "_blank", class: "btn btn-large btn-primary"
Run Code Online (Sandbox Code Playgroud)
编辑:好的,明白了.我建议不要重写这种常用方法,以便创建另一个方法:
def link_to_blank(body, url_options = {}, html_options = {})
link_to(body, url_options, html_options.merge(target: "_blank"))
end
Run Code Online (Sandbox Code Playgroud)
它应该做的伎俩
添加到 Anthony 的答案中,这更类似于 Rails 的link_to实现,包括对块的支持和不传递参数:
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
43721 次 |
| 最近记录: |