I know it's apparently a frequent question (saw many replies here), but I couldn't fix it yet. So.. I'm building a simple blog app with devise gem, but when I try to log in, I always get the "Invalid Email or password." message :/

Actually, this is my first real project with rails, so sometimes I'm kinda lost with routes, and etc... By the way, I would say the error is probably being caused by the routes file. Please, I'd appreciate some help :)

My routes.rb file:

Rails.application.routes.draw do

  root 'posts#index'

  resources :posts do
    resources :comments
  end 

  devise_for :users
  devise_scope :user do
    get "login", :to => "devise/sessions#new"
    get "logout",   :to => "devise/sessions#destroy"
  end

My sessions/new.html.erb file:

<div class="container mt-3 pb-3">
  <div class="row">
    <div class="col-lg-6 border">
      <h1>Login</h1>

        <%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
          <div class="form-group">
            <%= label_tag :email, "Email" %>
            <%= text_field_tag :email, "", autofocus: true, :class =>"form-control" %>
          </div>
          <div class="form-group">
            <%= label_tag :password, "Senha" %>
            <%= password_field_tag :password, "", :class =>"form-control" %>
          </div>
          <div class="form-group">
            <%= submit_tag "Entrar", :class =>"form-control btn btn-outline-dark" %>
          </div>
        <% end %>

      <%= render "devise/shared/links" %>

    </div>
  </div>
</div>

My registrations/_form.html.erb:

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
  <%= render "devise/shared/error_messages", resource: resource %>

  <div class="form-group">
    <%= f.label :username, "Nome" %>
    <%= f.text_field :username, autofocus: true, autocomplete: "username", :class =>"form-control" %>
  </div>

  <div class="form-group">
    <%= f.label :email, "Email" %>
    <%= f.email_field :email, autocomplete: "email", :class =>"form-control" %>
  </div>

  <div class="form-group">
    <%= f.label :password, "Senha" %>
    <% if @minimum_password_length %>
    <em>(No mínimo <%= @minimum_password_length %> caracteres)</em>
    <% end %><br />
    <%= f.password_field :password, autocomplete: "new-password", :class =>"form-control" %> 
  </div>

  <div class="form-group">
    <%= f.label :password_confirmation, "Confirme sua senha" %><br />
    <%= f.password_field :password_confirmation, autocomplete: "new-password", :class =>"form-control" %>
  </div>

  <div class="form-group">
    <%= f.submit "Criar", :class =>"form-control btn btn-outline-dark" %>
  </div>
<% end %>

JUST ANOTHER THING! MAYBE IT'S IMPORTANT...

I have a navbar rendered in "layouts/application.html.erb" that shows the current user's name and a logout button when there's a user connected, and a login button when there are no sessions.

<% unless current_page? (login_path) %>
    <%= link_to 'Entrar', login_path, :class =>"btn btn-outline-light" %>
<% end %>

1 Answers

0
Yuriy Verbitskiy On Best Solutions

Your error was caused by wrong sign_in form. I have changed this part

        <%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
          <div class="form-group">
            <%= label_tag :email, "Email" %>  
            <%= text_field_tag :email, "", autofocus: true, :class =>"form-control" %>
          </div>
          <div class="form-group">
            <%= label_tag :password, "Senha" %>
            <%= password_field_tag :password, "", :class =>"form-control" %>
          </div>
          <div class="form-group">
            <%= submit_tag "Entrar", :class =>"form-control btn btn-outline-dark" %>
          </div>
        <% end %>

To this one, and everything worked.

        <%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
          <div class="form-group">
            <%= f.label :email, "Email" %>
            <%= f.text_field :email, autofocus: true, :class =>"form-control" %>
          </div>
          <div class="form-group">
            <%= f.label :password, "Senha" %>
            <%= f.password_field :password, :class =>"form-control" %>
          </div>
          <div class="form-group">
            <%= f.submit "Entrar", :class =>"form-control btn btn-outline-dark" %>
          </div>
        <% end %>