开发者

Change route with or without login in Rails

开发者 https://www.devze.com 2023-03-19 12:42 出处:网络
Like the title says, i would like to change the route depending on if there is a user开发者_开发百科 logged in or not, similarly to facebook. Is this possible?You don\'t change the route, you just add

Like the title says, i would like to change the route depending on if there is a user开发者_开发百科 logged in or not, similarly to facebook. Is this possible?


You don't change the route, you just add a :before_filter in your controller. There's a few ways to do it, but a simple example is pretend your root points at homeController#main. On top of the controller you have something like

before_filter :check_login, :only => {:main}

And then in your ApplicationController you write a method like

def check_login
    if !session[:userid].nil
        redirect_to :controller => :users, :action => :profile
    end
end

Which will check your session variable for a user and redirect to their profile if it exists, if not, then it will render your main method normally.

More information and examples here.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号