This snippet works fine:
<%= form_tag(session_path) do |f| %>
<p>
<%= label_tag :email %>
<%= email_field_tag :email, nil %>
</p>
<p>
<%= label_tag :password %>
<%= password_field_tag :password, nil %>
</p>
<%= submit_tag "Sign in", class: 'btn-primary' %>
<% end %>
But when I tried to refactor it to simple_form_for
as per screenshot, it's giving me error Invalid email or password!
# sessions_controller.rb
class SessionsController < ApplicationController
def new
end
def create
if user = User.authenticate(params[:email],params[:password])
session[:user_id] = user.id
flash[:notice] = "Signed in successfully."
redirect_to user
else
flash.now[:alert] = 'Invalid email or password!'
render :new
end
end
end
I'm feeling like there's something wrong in the line
<%= simple_form_for :session, url: session_path do |f| %>
In routes.rb
: resource :session
- Yes, it's singular, not plural
When you added simple_form_for :session
the form started to send all parameters wrapped in the session
key: session: { email: 'email@email.com', password: 'password' }
. You'll see it if you check server log
Change in the controller:
User.authenticate(params[:session][:email], params[:session][:password])