这里很好地描述了问题
提取:
Given
我正在使用具有 Devise 身份验证的 Rails 应用程序
And
我已注册
But
我现在没有登录
When
我访问路径"/路径#事物">
Then
我应该看到登录表单
When
登录
Then
我应该被重定向到"/path#thing">
在您的登录页面中添加一个隐藏的输入,对我来说它是app/views/devise/sessions/new.html.haml
还有一些JavaScript来填充输入:
我调用了输入tab_hash
= simple_form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f|
= f.input :login, :hint => true, :required => true
= f.input :password, :as => :password, :hint => false, :required => true
= f.input :tab_hash, :as => :hidden
%div.text-right
= f.button :submit, t("formtastic.actions.sign_in"), class: 'btn-primary'
.text-center
%p
= render :partial => "devise/shared/links"
:javascript
$(document).ready( function(){
$("#user_tab_hash").val(window.location.hash);
});
将attr_accessor :tab_hash
添加到用户模型
在应用程序/控制器/application_controller.rb 中
加
def after_sign_in_path_for(resource)
stored = stored_location_for(resource)
if stored.present?
stored += params['user']['tab_hash'] # refs #3485 after signing in the tab was lost
end
stored || root_url
end