即使登录正确,Devise登录也会始终返回“无效的电子邮件或密码”

时间:2019-04-13 18:53:54

标签: ruby-on-rails ruby devise

我知道这显然是一个常见问题(在这里看到了很多答复),但是我还不能解决。 所以..我正在用devise gem构建一个简单的博客应用程序,但是当我尝试登录时,我总是会收到“无效的电子邮件或密码”。讯息:/

实际上,这是我第一个使用Rails的真实项目,因此有时我有点迷路,等等。。。顺便说一句,我会说错误可能是由route文件引起的。 拜托,谢谢您的帮助:)

我的routes.rb文件:

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

我的session / new.html.erb文件:

<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>

我的注册信息/_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 %>

另一件事!也许很重要...

我在“ layouts / application.html.erb”中呈现了一个导航栏,该导航栏显示了当前用户的名称,并在连接用户时显示了注销按钮,在没有会话时显示了登录按钮。

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

1 个答案:

答案 0 :(得分:0)

您的错误是由错误的sign_in格式引起的。 我已经更改了这部分

        <%= 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 %>

对此,一切正常。

        <%= 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 %>