渲染和/或重定向Ruby

ztigrdn8  于 2023-01-30  发布在  Ruby
关注(0)|答案(2)|浏览(125)

我有2个会话客户端和管理员登录页面,所以当客户端登录我重定向完成表格,当管理员登录我重定向他到 Jmeter 板,问题是:
ActionController::DoubleRenderError(在此操作中多次调用了Render和/或redirect。请注意,您只能调用render或redirect,并且每个操作最多调用一次。另请注意,redirect或render都不会终止操作的执行,因此,如果要在重定向后退出操作,则需要执行类似“redirect_to(...)and return”的操作。)

def login
    @institute = Configuration.find_by_config_key("LogoName")
    available_login_authes = FedenaPlugin::AVAILABLE_MODULES.select{|m| m[:name].classify.constantize.respond_to?("login_hook")}
    selected_login_hook = available_login_authes.first if available_login_authes.count>=1
    if selected_login_hook
      authenticated_user = selected_login_hook[:name].classify.constantize.send("login_hook",self)
    else
      if request.post? and params[:user]
        @user = User.new(params[:user])
        user = User.find_by_username @user.username
        if user.present? and User.authenticate?(@user.username, @user.password)
          authenticated_user = user 
        end
      end
    end
    if authenticated_user.present?
      successful_user_login(authenticated_user) and return
    elsif authenticated_user.blank? and request.post?
      flash[:notice] = "#{t('login_error_message')}"
    end
  end

  private
  def successful_user_login(user)
    session[:user_id] = user.id
    flash[:notice] = "#{t('welcome')}, #{user.first_name} #{user.last_name}!"
    redirect_to session[:back_url] || {:controller => 'user', :action => 'dashboard'}
    if user.client
      redirect_to session[:back_url] || {:controller => 'client', :action => 'complete_registration'}
    end
  end
end
91zkwejq

91zkwejq1#

你说右边的redirect_to不是返回方法,上面的代码也是执行的,在你的successful_user_login中如果用户是客户端,你调用redirect_to两次,所以你可以重构这个方法为:

if user.client
  redirect_to session[:back_url] || {:controller => 'client', :action => 'complete_registration'}
else
  redirect_to session[:back_url] || {:controller => 'user', :action => 'dashboard'}
end
ccgok5k5

ccgok5k52#

ActionController::DoubleRenderError是Ruby on Rails在一个控制器操作两次尝试呈现或重定向响应时抛出的错误。操作尝试将用户重定向到“user/dashboard”和“client/complete_registration”,具体取决于用户的客户端状态。这将导致DoubleRenderError。请注意,呈现或重定向不会从方法返回。
下面是一些使用不同方法的未经测试的代码示例:
您可以在调用redirect_to之前计算控制器和操作:

def successful_user_login(user)
  session[:user_id] = user.id
  flash[:notice] = "#{t('welcome')}, #{user.first_name} #{user.last_name}!"

  controller, action = if user.client
    ["client", "complete_registration"]
  else
    ["user", "dashboard"]
  end

  redirect_to session[:back_url] || {controller: controller, action: action}
end

也可以执行if并在第一个redirect_to之后返回:

def successful_user_login(user)
  session[:user_id] = user.id
  flash[:notice] = "#{t('welcome')}, #{user.first_name} #{user.last_name}!"

  if user.client
    redirect_to session[:back_url] || {:controller => 'client', :action => 'complete_registration'}
    return
  end

  redirect_to session[:back_url] || {:controller => 'user', :action => 'dashboard'}
end

或者执行if else:

def successful_user_login(user)
  session[:user_id] = user.id
  flash[:notice] = "#{t('welcome')}, #{user.first_name} #{user.last_name}!"

  if user.client
    redirect_to session[:back_url] || {:controller => 'client', :action => 'complete_registration'}
  else
    redirect_to session[:back_url] || {:controller => 'user', :action => 'dashboard'}
  end
end

相关问题