Ruby on Rails app planning - login system routing when not logged in -
Ruby on Rails app planning - login system routing when not logged in -
i'm building rails app, , far i've set pretty basic user registration/login system, next railcast found stack overflow. i've left same railcast, used strong parameters instead of attr_accessible , added additional fields (username, bio, img url) table.
now want app redirect users onto login page if they're not logged in, no matter page seek access, , if are, redirect normal path. loging page root_path. need in controllers separately or can write appcontroller? how go writing controller? thinking this:
if session[:user_id] == nil redirect_to login_path else redirect_to current_controller_path end
now how check if user logged in, , how redirect current controller path (for instance articles_index_path?
i new ruby on rails, , still trying wrap head around models, views , controllers, please assume know nil when writing explanations :) help
oh i'm using rails 4 ruby 2.2.1
you need add together before_filter
in applicationcontroller
check user's authentication.
class applicationcontroller < actioncontroller::base ... before_filter :authenticate_user! ... private def authenticate_user! redirect_to login_path unless session[:user_id] end end
now create sure user should logged in accessing action of controller, including signup, signin , other actions should accessible non-logged in users too.
you need create sure skip above before_filter
don't want user logged in such signup, signin, us, contact etc actions this.
for example:
class sessionscontroller < applicationcontroller skip_before_filter :authenticate_user!, :except => :destroy ... def new ... end def create ... end ... end
you can read more skip_before_filter
on apidock
ruby-on-rails login
Comments
Post a Comment