Learn
Well done!
- When you visit the URL
/login
, the browser makes a GET request for the URL. This request hits the Sessions controller’snew
action, which returns a view displaying the login page. - In the login form, we use
form_for(:session, url: login_path) do |f|
. This refers to the name of the resource and corresponding URL. In the signup form, we usedform_for(@user) do |f|
since we had a User model. For the login form, we don’t have a Session model, so we refer to the parameters above.
Instructions
1.
Next, let’s take in data submitted through the form and log the user in by starting a new session.
In the routes file, create a route that maps the URL ‘/login’ to the Sessions controller’s create
action.
post 'login' => 'sessions#create'
2.
In the Sessions controller, add the create
action
def create @user = User.find_by_email(params[:session][:email]) if @user && @user.authenticate(params[:session][:password]) session[:user_id] = @user.id redirect_to '/' else redirect_to 'login' end end
3.
Visit http://localhost:8000/login
and log in with your email and password.
Sign up to start coding
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.