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
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
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?
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. Here is the issue:
if @valid
sign_up
end
This calls your sign_up
method which attempts to render a response. 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.
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:
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.