简体   繁体   中英

Page isn't redirecting properly Ruby on Rails

For some reason, I am getting an error when clicking on a protected part of my page:

Firefox: The page isn't redirecting properly

Here is the method I use in my ApplicationController :

protected
  def authorize
    unless User.find_by_id(session[:remember_token])
      flash[:notice] = "Please Log in"
      redirect_to :controller => 'sessions', :action => 'new'
    end
  end

For some reason, I am not getting access to sessions/new , which is my login page. Any help would be very much appreciated. I checked routes and I have get sessions/new .

As a well educated guess, I'd say you have this:

before_filter :authorize

Which will keep redirecting to itself.

You can fix this either by passing only the ones you want:

before_filter :authorize, :only => [:action_a, :action_b]

Or specify the ones you don't want (probably preferable if you only want to stop your sessions#new action

before_filter :authorize, :except => [:new, :create]

If your before_filter is specified on an inherited controller (eg your SessionsController is inheriting the before_filter from ApplicationController) then you can use skip_before_filter

skip_before_filter :authorize, :only => [:new, :create]

I had a similar issue and the main reason this happens is when we are redirecting to itself. make sure you are not applying the before_action / before_filter to Session#new

before_filter :authorize, :except => [:new, :create]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM