3

I want to redirect a user if a condition is true:

class ApplicationController < ActionController::Base
  @offline = 'true'
  redirect_to :root if @offline = 'true'
  protect_from_forgery
end

Edit This is what I'm trying now with no success. The default controller is not the Application Controller.

class ApplicationController < ActionController::Base
    before_filter :require_online 

    def countdown

    end

private
  def require_online
    redirect_to :countdown
  end

end

This gives a browser error Too many redirects occurred trying to open “http://localhost:3000/countdown”. This might occur if you open a page that is redirected to open another page which then is redirected to open the original page.

If I add && return, the action never gets called.

Kevin Brown
  • 12,602
  • 34
  • 95
  • 155

3 Answers3

4

In addition to Jed's answer

Need the comparison operator, not the assignment operator:

 redirect_to :root if @offline == 'true'

If you're having further difficulties, simplify tests with:

 redirect_to(:root) if @offline == 'true'

Or maybe it should be a true boolean and not a string?

 redirect_to :root if @offline

class ApplicationController < ActionController::Base
   before_filter :require_online 

 private
    def require_online
       redirect_to(:root) && return if @offline == 'true'
    end
 end
Mohit Jain
  • 43,139
  • 57
  • 169
  • 274
  • I like this approach. How would I redirect_to a function within the `ApplicationController`? I'm completely new to rails. :) – Kevin Brown Jul 01 '11 at 23:23
  • I may be wrong, but you can't call actions in the ApplicationController, right? I'm still getting errors. – Kevin Brown Jul 02 '11 at 23:40
  • @kevin you can call redirect_to in application controller. Whats the error that u are getting? – Mohit Jain Jul 04 '11 at 22:01
  • sorry for the late response. Please see my edit. The redirect isn't getting called at all, it seems. I've tried adding `&& return` as well. – Kevin Brown Jul 08 '11 at 23:57
  • @kevin countdown action is having some before filter or something whcih is redirecting it to new page.. and from there its redirecting to countdown action.. Its multiple redirects.. kind of loop for redirects.. – Mohit Jain Jul 10 '11 at 19:03
  • got it. How do I keep it from looping? – Kevin Brown Jul 10 '11 at 20:11
  • countdown action doesnt require that require_online?? if yes then in that controller skip this before filter something like: skip_before_filter :require_online, :only=>[:countdown] – Mohit Jain Jul 10 '11 at 20:39
  • That doesn't make much sense to me--I'm a rails novice and don't really understand exceptions/exclusions/inclusion phrases like that. Will you please expand? – Kevin Brown Jul 10 '11 at 20:45
  • I used `:except => :countdown` – Kevin Brown Jul 10 '11 at 21:21
1

Redirect_to should be called from an action.

As an example,

class ApplicationController < ActionController::Base
  protect_from_forgery

  def index
    @offline = 'true'

    // Once you do the redirect make sure you return to avoid a double render error. 
    redirect_to :root && return if @offline == 'true' // Jed was right use a comparison 
  end
end

Take a look at the 'Redirect' docs

diedthreetimes
  • 4,086
  • 26
  • 38
  • As an aside. You are also using an instance variable inside the controller class definition, which is not accessible from the actions. If you want similar behavior (persists across actions) you may want to use a class variable `@@offline`, but my guess is you don't ;) – diedthreetimes Jul 01 '11 at 19:37
0

Need the comparison operator, not the assignment operator:

redirect_to :root if @offline == 'true'

If you're having further difficulties, simplify tests with:

redirect_to(:root) if @offline == 'true'

Or maybe it should be a true boolean and not a string?

redirect_to :root if @offline
glortho
  • 13,120
  • 8
  • 49
  • 45