sparkle
sparkle

Reputation: 7400

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'

Upvotes: 4

Views: 3360

Answers (3)

sparkle
sparkle

Reputation: 7400

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'

Upvotes: 9

Rekha Benada
Rekha Benada

Reputation: 9

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

Upvotes: 0

engineerDave
engineerDave

Reputation: 3935

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

Like in this answer

Upvotes: 0

Related Questions