Question

I am trying to open a page when I go through an action. So in my controller I have the following:

class Controller < ApplicationController
  require 'open-uri'

  def action
    open("http://www.google.com") #if i use this, it just goes to my action page
    open("www.google.com") #if i use this, it gives the error
  end
end

the error:

Errno::ENOENT in Controller#action
  No such file or directory - www.google.com

All the solutions I have found just said to include require 'open-uri', which I believe I did. What am I doing wrong here?

EDIT:
Sorry I should have been more clear. I may have to open multiple pages in new tabs, which is why I decided to go with open instead of redirect_to. For example:

class Controller < ApplicationController
  require 'open-uri'

  def action
    open("http://www.google.com")
    open("http://www.yahoo.com")
  end
end

where doing that would open both google.com and yahoo.com.

Was it helpful?

Solution

open is generally used for having your server access an external site (and probably parse the response). It does not allow you to redirect your user. In order to interact with the client, you need to use your view + javascript.

<%= link_to @url, target: "_blank" %> for each page you want, then clicking on them automatically via javascript.

For more on that, see: How do I programmatically click a link with javascript?

Then display the links as a fallback if the user doesn't have JS enabled. See also this question: Ruby/Rails - Open a URL From The Controller In New Window

OTHER TIPS

You have to use probably the protocol:

http://....
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top