I'm using the restful authentication plugin to authenticate users for an application I'm building. As in most good sites when a user visits the homepage I want to show a boilerplate welcome page and have the option to login or signup. However when a user is logged in I want t开发者_开发技巧he root url of the site (i.e. sitename.com ) to show a user dashboard instead of the standard greeting page. I'm assuming this is simply accomplished using layout files and checking to see if the user is logged_in? but the logic of it seems to be stumping me. Many thanks in advance.
I would do it like so: you'd have your main
controller which non-logged in users can access, and then a second controller (let's call it admin/main
) which is the main page for logged in users.
In the index
action of the main
controller (assuming you just want to redirect the index action; otherwise, you could use a before_filter
):
class MainController < ApplicationController
def index
redirect_to :controller => 'admin/main', :action => 'index' and return if logged_in?
end
end
This way, if a logged in user tries to access the root URL of your application, they will be automatically redirected to the logged-in area of the site (but the URL won't change).
You can check whether the user is already logged in when they hit your login action and, if they are redirect to your dashboard action. If they aren't then you can carry on with logging in as normal. E.g. in your sessions controller
def new
if logged_in?
redirect_to :controller => 'dashboard', :action => 'index'
end
end
You could also do it in a before_filter to avoid 'polluting' your login action. E.g.
before_filter :logged_in_users_go_to_dashboard, :only => [:create, :new]
def logged_in_users_go_to_dashboard
redirect_to(:controller => 'dashboard', :action => 'index') if logged_in?
end
精彩评论