[英]Double render error when trying to auto sign in and redirect user into a specific path after registration in Devise
Hi I am getting a double redirection here 嗨,我在这里得到了双重重定向
What I've been trying to accomplish is to auto sign in the user and redirect the user into a specific path, not in the root path. 我一直试图实现的是自动登录用户并将用户重定向到特定路径,而不是根路径。
In my registrations_controller.rb 在我的registrations_controller.rb中
def new
@user = User.new
end
def create
@user = User.new params[:user]
@valid = @user.save
if @valid
sign_up
end
respond_to do |format|
format.js { render layout: false }
end
end
def sign_up
sign_in(resource_name, resource)
respond_with resource, :location => after_sign_up_path_for(resource)
end
In my application_controller.rb 在我的application_controller.rb中
def after_sign_in_path_for(resource)
if resource.admin? or resource.super_admin?
admin_index_path
else
if mobile_device?
page_path("backgroundinfo")
else
page_path("howto")
end
end
end
def after_sign_up_path_for(resource)
page_path("howto")
end
Any workarounds will be appreciated. 任何解决方法将不胜感激。
PS: Removing the respond_with resource, :location => after_sign_up_path_for(resource)
is still redirecting the user to the root_path
not in the page_path
maybe because the controller action is in registration? PS:删除
respond_with resource, :location => after_sign_up_path_for(resource)
仍将用户重定向到不在page_path
的root_path
,也许是因为控制器操作正在注册中?
A double render error happens when you try to render two templates from the same request. 当您尝试从同一请求呈现两个模板时,会出现双重呈现错误。
Your sign_up
method attempts to respond with a respond_with
block, and after that your create
method attempts to make another response from the respond_to
block. 你
sign_up
方法尝试与响应respond_with
块,在那之后你create
方法试图使从另一个响应respond_to
块。 Here is the issue: 这是问题所在:
if @valid
sign_up
end
This calls your sign_up
method which attempts to render a response. 这将调用您的
sign_up
方法,该方法尝试呈现响应。 After that happens, the following block tries to render another template: 在这种情况发生之后,以下块尝试呈现另一个模板:
respond_to do |format|
format.js { render layout: false }
end
Your execution path attempts two render
calls, which gets you a double render error. 您的执行路径将尝试两次
render
调用,这将导致您出现两次渲染错误。
Your code should look like something along these lines: 您的代码应类似于以下几行:
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
# sign in user here and render response for success
format.js { #handle js }
format.html { #handle html }
else
# user was not saved
format.js { #handle error }
format.html { #handle error }
end
end
end
I had the same issue, and bypassed it by overriding Devise::SessionsController, like so: 我遇到了同样的问题,并通过重写Devise :: SessionsController绕过了它,如下所示:
class SessionsController < Devise::SessionsController
def new
self.resource = resource_class.new(sign_in_params)
clean_up_passwords(resource)
#yield resource if block_given?
#respond_with(resource, serialize_options(resource))
# The rest of your code
# your redirect
redirect_to '/'
end
I'm unsure as to whether this has any negative side affects, but it appears to work fine, and also stops it from appending user sign-in data to the URL. 我不确定这是否有任何负面影响,但它似乎可以正常工作,并且还可以阻止将用户登录数据附加到URL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.