ruby-on-rails-3 – 登录重定向后设计 – 双重渲染错误
发布时间:2020-12-17 01:46:40 所属栏目:百科 来源:网络整理
导读:我正在使用rails 3.2和Devise(最新版本) 主要想法是在登录后测试当前登录用户的一些变量.因此,例如,如果用户有待创建地址我想重定向新地址路径.但我得到的是双重渲染错误. 这是代码 class ApplicationController ActionController::Base protect_from_forger
我正在使用rails 3.2和Devise(最新版本)
主要想法是在登录后测试当前登录用户的一些变量.因此,例如,如果用户有待创建地址我想重定向新地址路径.但我得到的是双重渲染错误. 这是代码 class ApplicationController < ActionController::Base protect_from_forgery # Devise: Where to redirect users once they have logged in def after_sign_in_path_for(resource) if current_user.is? :company_owner if $redis.hget(USER_COMPANY_KEY,current_user.id).nil? redirect_to new_owner_company_path and return else @addr_pending = $redis.hget(PENDING_ADDRESS_KEY,current_user.id) unless @addr_pending.nil? || !@addr_pending redirect_to owner_company_addresses_path and return end end end root_path end end 我的路线定义 root :to => "home#index" devise_for :users,:controllers => { :omniauth_callbacks => "users/omniauth_callbacks" } resources :users,:only => :show namespace :owner do resource :company do # single resource /owner/company get 'thanks' get 'owner' #TODO: esto hay que sacarlo de aquí y forme parte del login resources :addresses end end 所以,当我用一个创建了pedding地址的用户登录时,我得到了 "render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect,and at most once per action. Also note that neither redirect nor render terminate execution of the action,so if you want to exit an action after redirecting,you need to do something like "redirect_to(...) and return". 出什么问题了? redirect_to owner_company_addresses_path and return 所以,我只想重定向到新的地址路径.我不明白为什么我得到错误. 提前致谢. —-编辑—- 似乎只返回一个路径(我认为使用redirect_to并返回就足够了,但事实并非如此) def after_sign_in_path_for(resource) @final_url = root_path if current_user.is? :company_owner if $redis.hget(USER_COMPANY_KEY,current_user.id).nil? @final_url = new_owner_company_path else @addr_pending = $redis.hget(PENDING_ADDRESS_KEY,current_user.id) unless @addr_pending.nil? || !@addr_pending @final_url = owner_company_addresses_path end end end @final_url end 解决方法
您应该删除redirect_to方法调用和return语句. after_sign_in_path_for应该只返回一个路径:
例如: def after_sign_in_path_for(resource) new_owner_company_path end (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |