0

I believe this is a super simple question for Ruby developers, although I am an expert in Java, but today is my first day in Ruby.

My code:

url="http://www.google.com"
redirect_to url

Error:

undefined method `redirect_to' for main:Object (NoMethodError)

Now, an educational guess is that I need to "require [some magical library]", but I search the web and stackoverflow:

"ruby redirect_to require" "ruby redirect_to doc" "ruby redirect_to lib" "ruby which library is redirect_to in" "ruby example code redirect_to" "ruby tutorial redirect_to" "open sesame"

and heck, nothing obvious which tells me this simple step to make a method call works. And it forced me to write this whole thing here.

Could you kindly give me a full piece of example code which can have the redirect_to method behave? Or better yet, tells me what keywords I should search for this kind of similar questions.

  • 2
    `redirect_to` is Rails, not ruby, and must be called in the context of a controller action. http://api.rubyonrails.org/classes/ActionController/Redirecting.html#method-i-redirect_to – sevenseacat Jun 05 '14 at 07:42
  • I guess the big question is... where are you trying to call redirect_to from? :) If it's in a controller - as suggested, then it will work... but otherwise it won't and we can talk about what you can do instead. – Taryn East Jun 05 '14 at 07:47
  • If you tell us more about what web framework you're using (if it isn't Rails) then that'll help us to find the equivalent of `redirect_to` – Sam Starling Jun 05 '14 at 07:59
  • Hey, I was expecting it to magically work, that is the whole script I had, and it is in a file test.rb. Now that I see you guys mention Rails, action framework and all that, I think I got it, I shouldn't expect Ruby to behave the same as I expect Perl to. I rewrote my simple script to just puts "" and it serve my need already. Perhaps another day for the Rails framework. – Michael Dang Jun 05 '14 at 08:31

1 Answers1

1

You can't just require something to get this because the very idea of a redirect doesn't make sense outside an "action". Only an action processes an HTTP request and sends a response*, and a redirect is a type of response. So as others have said, call this in an action and it will work. Call it anywhere else and it won't be defined because it is the action/controller system that defines it.

Since you come from Java, a reasonable analog would be to ask what you need to import to be able to call toUpperCase from a subclass of IOStream. !?

(* this is a bit of a lie, but true enough. There are a couple of non-action ways to respond to a request in rails, but redirect_to is part of ActionController.)

gwcoffey
  • 5,551
  • 1
  • 18
  • 20
  • Thanks for the great explanation. I tried out Ruby today and thought that it act like my Perl script, so I think I mistakenly use it as a quick scripting tool to do my page forwarding. I probably used a nail gun for my sticky notes. Anyway, thanks for the help and I got it working by simply doing puts html meta redirect hack. – Michael Dang Jun 05 '14 at 08:37
  • You might be interested in http://stackoverflow.com/questions/1895683/is-there-a-phpish-way-of-doing-webpages-with-ruby/1901772#1901772 – gwcoffey Jun 05 '14 at 08:55