为什么在around_filter或after_filter中的redirect_to不起作用?

如何使redirect_to在这些filter中起作用?

我想改变

def start .... redirect_to index end def end ... redirect_to index end 

 around_filter :around def around ... yield redirect_to index end def start .. end def stop ... end 

操作完成后,它会自动呈现模板,因此在请求完成后无法呈现/重定向。 您可以通过将redirect_to放在所需操作的末尾来解决此问题。 这不是around_filters的设计目的。

据推测,您的操作已经有redirect_torender调用。 您不能每次请求两次调用这些方法。

您可以更改response.location ,它与调用redirect_to具有相同的效果。 使用after_filter的示例(可以使用相同的方法):

  after_filter :different_redirect, only:[:create] def different_redirect if self.status == 302 response.location = other_thing_path end end def create @my_thing = MyThing.new(params[:my_thing]) respond_to do |format| if @my_thing.save format.html { redirect_to(my_things_path) } else format.html { render :action => "new" } end end end