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?
For example
def link
@url = 'www.google.com'
****??? Open @url ?? *** with target blank?
end
This is not possible to do directly from the controller. Using
redirect_to @urlhas the effect of opening an URL in the same “window”, as it simply sends a HTTP redirect instruction back to the browser.redirect_tois not capable of opening new windows. The controller resides on the server side, and opening a new window belongs to the client side.Some options:
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 viewb) use JavaScript to open the link automatically, but beware that browsers may treat this as a popup and block it
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.