如何检查控制器操作是否已重定向?

时间:2022-12-01 22:43:51

OK, as is often the case, I have a controller action in my app that is protected from unauthorized access by a before_filter. The only thing is that I need to redirect this action if another condition is true:

好的,通常情况下,我在我的应用程序中有一个控制器操作,可以防止before_filter的未经授权的访问。唯一的问题是,如果另一个条件为真,我需要重定向此操作:

class Payment < ApplicationController
  before_filter login_required

  def new
    redirect_to some_other_path if @order.is_free?
    @payment = Payment.new
  end
end

In my testing, I check to make sure that the action is correctly protected, however, it is also the case that the @order.is_free statement is true. When this is the case, I get the following error:

在我的测试中,我检查以确保操作被正确保护,但是,@ order.is_free语句也是如此。在这种情况下,我收到以下错误:

`render_with_no_layout': Can only render or redirect once per action

Is there any way of checking to make sure I'm not already redirecting or to override an existing redirect?

有没有办法检查以确保我还没有重定向或覆盖现有的重定向?

4 个解决方案

#1


I am assuming that the login_required method performs a redirect if the user is not logged in. In which case:

我假设如果用户没有登录,login_required方法会执行重定向。在这种情况下:

Your before filter should return false after calling redirect. This will prevent the new action from ever being called. Later versions of rails automatically do this if you call render or redirect in a before_filter, so maybe you are using an older version.

调用重定向后,您的前置过滤器应返回false。这将阻止调用新操作。如果您在before_filter中调用渲染或重定向,则更高版本的rails会自动执行此操作,因此您可能正在使用旧版本。

Also you should return after the call to redirect in the new handler, unless you want to always create a new Payment object.

此外,您应该在新处理程序中重定向调用后返回,除非您想要始终创建新的Payment对象。

#2


Your class should be PaymentController, not Payment. The reason for this is so the controller class and model class do not *.

你的班级应该是PaymentController,而不是Payment。这样做的原因是控制器类和模型类不会发生冲突。

#3


I don't think your before filter is what causes the double render error. Take a look at this example:

我不认为你的前置过滤器是导致双重渲染错误的原因。看看这个例子:

class PostsController < ApplicationController
  before_filter :perform_a_redirect, :except => [:wtf]

  def index
    redirect_to 'http://google.com'
  end

  def wtf
    render :text => 'wtf'
  end

  private

  def perform_a_redirect
    redirect_to :action => 'wtf'
  end
end

When visiting /posts, I get redirected to /posts/wtf. No double render error. Assuming your 'login_required' method only redirects/renders once, I suspect that the code you are posting here is not the problem, but that something else is causing this.

访问/发布时,我被重定向到/ posts / wtf。没有双重渲染错误。假设你的'login_required'方法只重定向/渲染一次,我怀疑你在这里发布的代码不是问题,但是其他东西导致了这个问题。

#4


The before filter is a red herring. When @order_is_free? the code is both setting up a redirect and falling through to a rendering of new. The redirect statement doesn't control the flow of the method. Add a return statement after the redirect, or you can even return the redirect, as in return(redirect_to :action => :show, :id => @order, :controller => :free_orders)

前过滤器是红鲱鱼。当@order_is_free?代码既设置了重定向又落到了新的渲染中。 redirect语句不控制方法的流程。在重定向之后添加一个return语句,或者你甚至可以返回重定向,如返回(redirect_to:action =>:show,:id => @order,:controller =>:free_orders)

#1


I am assuming that the login_required method performs a redirect if the user is not logged in. In which case:

我假设如果用户没有登录,login_required方法会执行重定向。在这种情况下:

Your before filter should return false after calling redirect. This will prevent the new action from ever being called. Later versions of rails automatically do this if you call render or redirect in a before_filter, so maybe you are using an older version.

调用重定向后,您的前置过滤器应返回false。这将阻止调用新操作。如果您在before_filter中调用渲染或重定向,则更高版本的rails会自动执行此操作,因此您可能正在使用旧版本。

Also you should return after the call to redirect in the new handler, unless you want to always create a new Payment object.

此外,您应该在新处理程序中重定向调用后返回,除非您想要始终创建新的Payment对象。

#2


Your class should be PaymentController, not Payment. The reason for this is so the controller class and model class do not *.

你的班级应该是PaymentController,而不是Payment。这样做的原因是控制器类和模型类不会发生冲突。

#3


I don't think your before filter is what causes the double render error. Take a look at this example:

我不认为你的前置过滤器是导致双重渲染错误的原因。看看这个例子:

class PostsController < ApplicationController
  before_filter :perform_a_redirect, :except => [:wtf]

  def index
    redirect_to 'http://google.com'
  end

  def wtf
    render :text => 'wtf'
  end

  private

  def perform_a_redirect
    redirect_to :action => 'wtf'
  end
end

When visiting /posts, I get redirected to /posts/wtf. No double render error. Assuming your 'login_required' method only redirects/renders once, I suspect that the code you are posting here is not the problem, but that something else is causing this.

访问/发布时,我被重定向到/ posts / wtf。没有双重渲染错误。假设你的'login_required'方法只重定向/渲染一次,我怀疑你在这里发布的代码不是问题,但是其他东西导致了这个问题。

#4


The before filter is a red herring. When @order_is_free? the code is both setting up a redirect and falling through to a rendering of new. The redirect statement doesn't control the flow of the method. Add a return statement after the redirect, or you can even return the redirect, as in return(redirect_to :action => :show, :id => @order, :controller => :free_orders)

前过滤器是红鲱鱼。当@order_is_free?代码既设置了重定向又落到了新的渲染中。 redirect语句不控制方法的流程。在重定向之后添加一个return语句,或者你甚至可以返回重定向,如返回(redirect_to:action =>:show,:id => @order,:controller =>:free_orders)