Ruby on rails RoR:忘记密码&;登录-在同一页
我试图将忘记密码字段放入登录页面,但如果用户未注册(且未在应用程序数据库中注册),则它会重定向到原始的设计忘记密码页面,并出现错误(Ruby on rails RoR:忘记密码&;登录-在同一页,ruby-on-rails,ruby,ruby-on-rails-4,devise,Ruby On Rails,Ruby,Ruby On Rails 4,Devise,我试图将忘记密码字段放入登录页面,但如果用户未注册(且未在应用程序数据库中注册),则它会重定向到原始的设计忘记密码页面,并出现错误(http://localhost:3000/users/password)。如何使错误显示在与登录页面相同的页面中(http://localhost:3000/users/sign_in) 在app/views/designe/sessions/new.html.erb文件中 <%= simple_form_for(resource, as: resourc
http://localhost:3000/users/password
)。如何使错误显示在与登录页面相同的页面中(http://localhost:3000/users/sign_in
)
在app/views/designe/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) 我必须在用户范围内覆盖Desive的密码控制器
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
这些操作将导致在用户登录表单下显示错误,但路径将保留。原因是我们正在呈现页面,而不是重定向。渲染只显示视图,而不执行控制器操作。现在,即使您尝试以某种方式(在覆盖该控制器之后)将错误消息发送到会话控制器,也会像这样
redirect_to new_user_session_path, :messages => resource.errors
但这仍然没有帮助,因为在会话#new中,我们正在重新初始化资源,因为它是新操作,所有错误都将消失
我不确定这是否令您满意,或者这是否不符合您的要求。我试着涵盖所有的事情。如果一些可信的或官方的消息来源能提供更好的回应,我将感到高兴。这肯定也会增加我的知识 你能发布控制器方法吗?@Pavan显然我没有针对用户或设计的控制器。。。自从尝试了这个,我只设计了用户。我应该创建一个独立于此的用户控制器吗?或者任何编码都可以进入
应用程序\u controller.rb
。如果是的话,我现在几乎什么都没有。只需一个def hello
,它只需呈现一个文本并使用::exception保护您免受伪造这是一个简洁的答案
<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 %>
devise_for :users, controllers: {
passwords: 'users/passwords'
}
redirect_to new_user_session_path, :messages => resource.errors