简体   繁体   中英

Rails landing page routing and devise

My website should works just like facebook.com. If the user is logged and if it goes to "/" it should be rendered home controller. If it isn't logged it should be render landing_page controller

"/ " && user_signed_in? ---> home controller

"/" && user_not_logged ---> landing_page controller

I'm using Rails 4 and Devise

ApplicationController

class ApplicationController < ActionController::Base

  before_filter :authenticate_user!
end

Routes.rb

get "landing_page/index"

root 'home#index', :as => :home

How I could keep a "before_filter" in ApplicationControl that run in every controllers except "landing_page" controller?

Update

If I go to "/en/landing_page" it render landing_page controller correctly (logged out), but if I go to "/" it redirect me to "/users/sign_in"

class LandingPageController < ApplicationController
  skip_before_action :authenticate_user!

  def index
  end

end

class ApplicationController < ActionController::Base

  before_action :authenticate_user!

end

Routes.rb

 root 'landing_page#index'

SOLVED!

LandingPageController

class LandingPageController < ApplicationController
  skip_before_action :authenticate_user!

  def index
  end

end

HomeController

class HomeController < ApplicationController
  skip_before_action :authenticate_user!
  before_action :check_auth

def check_auth
    unless user_signed_in?
        redirect_to :controller => :landing_page
    end
end
 end 

ApplicationController

class ApplicationController < ActionController::Base

  before_action :authenticate_user!

end

Routes.rb

 root 'landing_page#index'

I think you could easily add a before filter to handle this action.

Like in this answer

I think you can write confirm_logged_in function action in controller

before_filter :confirm_logged_in

In that function you can mention how you want to render pages based on logged in user

def confirm_logged_in<br>
  unless session[:user_id]
  flash[:notice] = 'Please log in.'
  redirect_to(:controller => 'access', :action => 'login')
  return false #halts the before_filter<

else
return true
  end
end

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