2

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.

notblakeshelton
  • 364
  • 1
  • 7
  • 20

2 Answers2

2

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

Community
  • 1
  • 1
Sean Ryan
  • 438
  • 4
  • 11
  • no, i may have to open more than one page. i think redirecting would just change one page multiple times. – notblakeshelton Jul 16 '13 at 22:11
  • You can't really do that in the controller. One option is redirecting to a view with a `<%= link_to @url, :target => "_blank" %>` each page you want, then clicking on them automatically via javascript. Then display the links as a fallback if the user doesn't have JS enabled. See also this question: http://stackoverflow.com/questions/7855629/ruby-rails-open-a-url-from-the-controller-in-new-window – Sean Ryan Jul 16 '13 at 22:15
  • Thanks for the explanation on `open`. I will just use your suggestion and click the links on a pageload of action.html.erb. May your sword stay sharp and your steed be quick. – notblakeshelton Jul 16 '13 at 22:30
1

You have to use probably the protocol:

http://....
BvuRVKyUVlViVIc7
  • 11,641
  • 9
  • 59
  • 111