Ruby/Rails - 在新窗口中从控制器打开 URL [英] Ruby/Rails - Open a URL From The Controller In New Window

查看:34
本文介绍了Ruby/Rails - 在新窗口中从控制器打开 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序控制器中,我有一个 url 字符串.如何告诉应用程序在新窗口中打开浏览器中的 url.有没有办法将目标设置为空白?

I'm in my applications controller and I have a url string. How do I tell the app to open the url in the browser in a new window. Is there a way to set the target to blank?

例如

def link
  @url = 'www.google.com'
  ****??? Open @url ?? *** with target blank?
end

推荐答案

这无法直接从控制器执行.使用 redirect_to @url 具有在同一窗口"中打开 URL 的效果,因为它只是将 HTTP 重定向指令发送回浏览器.redirect_to 无法打开新窗口.控制器驻留在服务器端,打开新窗口属于客户端.

This is not possible to do directly from the controller. Using redirect_to @url has the effect of opening an URL in the same "window", as it simply sends a HTTP redirect instruction back to the browser. redirect_to is not capable of opening new windows. The controller resides on the server side, and opening a new window belongs to the client side.

一些选项:

a) 使用 <%= link_to 'Google', 'google.com', :target => 呈现链接'_blank' %><a href="google.com" target="_blank">Google</a> 用户可以在新视图中点击

a) render a link with <%= link_to 'Google', 'google.com', :target => '_blank' %> or <a href="google.com" target="_blank">Google</a> which the user can click on in a new view

b) 使用 JavaScript 自动打开链接,但请注意浏览器可能会将其视为弹出窗口并阻止它

b) use JavaScript to open the link automatically, but beware that browsers may treat this as a popup and block it

通过组合这些选项,您可以在新窗口中为允许的浏览器/用户打开链接,并在无效时退回到常规 URL.

By combining these options you can open links in new window for browsers/users who allow it, and fall back to a regular URL in case that didn't work.

这篇关于Ruby/Rails - 在新窗口中从控制器打开 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆