ruby-on-rails - 目录 : Forgot Password & Sign In - in same page

标签 ruby-on-rails ruby ruby-on-rails-4 devise

我正在尝试将忘记密码字段与登录页面放在一起,但如果用户未注册(并且不在应用程序数据库中),则它会重定向到原始设计的忘记密码页面并出现错误 (http://localhost:3000/用户/密码)。如何使错误出现在与登录页面 (http://localhost:3000/users/sign_in) 相同的页面中?


app/views/devise/sessions/new.html.erb文件中

<%= simple_form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
  <%= f.email_field :email, required: false, autofocus: true, placeholder: "Username" %>
  <%= f.password_field :password, required: false, placeholder: "Password" %>
  <%= f.button :submit, "Sign In", class: "btn btn-success btn-sm" %>

  <div class="remember-forgot">
     <div class="row">
       <div class="col-md-6">
         <%= f.input :remember_me, as: :boolean if devise_mapping.rememberable? %>
       </div>
       <div class="col-md-6 forgot-pass-content">
         <a href="javascription:void(0)" class="forgot-pass">Forgot Password</a>
       </div>
     </div>
  </div>
<% end %>

<!-- where reset is -->

<div class="pass-reset">
  <%= simple_form_for(resource, as: resource_name, url: password_path(resource_name), namespace: 'forgot', html: { method: :post }) do |f| %>
    <%= f.error_notification %>
    <label>Enter the email you signed up with</label>
    <%= f.email_field :email, required: true, autofocus: true, placeholder: "Email" %>
    <%= f.submit "Submit", class: "pass-reset-submit btn btn-success btn-sm" %>
  <% end %> 
</div>

所以有一个 javascript 链接,如果用户忘记了他们的登录凭据,输入字段将显示在该链接上。

最佳答案

显然,具有相同字段的同一对象的两个表单不应该在一页上,一个应该留在新页面上。但我仍然尝试过你的问题,需要做以下事情

1) 我必须覆盖密码 Controller 以在用户范围内进行设计。

class Users::PasswordsController < Devise::PasswordsController
  # GET /resource/password/new
  def new
    super
  end

  # POST /resource/password
  def create
  self.resource = resource_class.send_reset_password_instructions(resource_params)

    if successfully_sent?(resource)
      flash[:notice] = "sent password"
      redirect_to :root
    else
      render "devise/sessions/new"
    end
  end

  # GET /resource/password/edit?reset_password_token=abcdef
  def edit
    super
  end

  # PUT /resource/password
  def update
    super
  end

  protected

  def after_resetting_password_path_for(resource)
    super(resource)
  end

  # The path used after sending reset password instructions
  def after_sending_reset_password_instructions_path_for(resource_name)
    super(resource_name)
  end
end

然后我的设计/ session /新页面将如下所示(您可以添加仅当单击忘记密码按钮时才显示表单的逻辑。这应该很简单。只需添加隐藏类并单击删除隐藏类。)

登录

<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
  <div class="field">
    <%= f.label :email %><br />
    <%= f.email_field :email, autofocus: true %>
  </div>

  <div class="field">
    <%= f.label :password %><br />
    <%= f.password_field :password, autocomplete: "off" %>
  </div>

  <% if devise_mapping.rememberable? -%>
    <div class="field">
      <%= f.check_box :remember_me %>
      <%= f.label :remember_me %>
    </div>
  <% end -%>

  <div class="actions">
    <%= f.submit "Log in" %>
  </div>
<% end %>

#假设 devise_mapping 可以恢复?选项。您也可以在 if 条件下保留以下表格

<h2>Forgot your password?</h2>

<%= form_for(resource, as: resource_name, url: password_path(resource_name), namespace: "forget", html: { method: :post }) do |f| %>
  <%= devise_error_messages! %>

  <div class="field">
    <%= f.label :email %><br />
    <%= f.email_field :email, autofocus: true %>
  </div>

  <div class="actions">
    <%= f.submit "Send me reset password instructions" %>
  </div>
<% end %>

需要告诉路由使用我的密码 Controller 。

devise_for :users, controllers: {
  passwords: 'users/passwords'
}

这些东西将导致在用户登录表单下显示错误,但路径将保留 http://localhost:3000/users/password .为什么因为我们正在渲染页面而不是重定向。 render 只显示 View 而不去 Controller 操作。现在即使有人试图以某种方式(也覆盖该 Controller 之后)将错误消息发送到 session Controller

    redirect_to new_user_session_path, :messages => resource.errors

这仍然无济于事,因为在 session#new 中我们正在重新初始化资源,因为它是新操作,所有错误都将消失。

我不确定这是否令您满意,或者这是否不符合您的要求。我试图涵盖所有的事情。如果一些可靠的或官方的消息来源能提供更好的回应,我会很高兴。那肯定也会增加我的知识。

关于ruby-on-rails - 目录 : Forgot Password & Sign In - in same page,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31483539/

相关文章:

ruby-on-rails - Rails 4 update_all 并从另一个字段设置值

ruby-on-rails - 在 Rails 中发送电子邮件时事通讯/调查

ruby-on-rails - 如何使用 Cucumber 和 RSpec 开发由外而内的 Rails 应用程序?

ruby - 当使用可执行二进制文件开发 gem 时,无法要求存在于 lib 目录中的文件

ruby - 引入关键字 "singleton_class"是否会破坏 Ruby 代码或导致其他困难?

ruby-on-rails-4 - Rails ActiveRecord 4.2 自定义排序顺序

ruby-on-rails - 如何在 Rails 中查找字符串长度?

ruby-on-rails - 在 Ubuntu 10.04 Lucid box 上使用 Nginx 实时发送 Rails 应用程序

ruby-on-rails - 如果 rails 中不存在路由,则重定向到根页面

ruby-on-rails - rails 4 : Set enum field through FactoryGirl attributes