Why redirect_to in around_filter or after_filter won't work?

后端 未结 3 672
天命终不由人
天命终不由人 2021-01-26 18:53

How to make redirect_to works in those filters?

I\'m trying to change

def start 
  ....
  redirect_to index
end

def end
  ...
  redirect_to index
end           


        
3条回答
  •  误落风尘
    2021-01-26 19:31

    You can change response.location, which has the same effect as calling redirect_to. An example with an after_filter (the same could be done with around):

      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
    

提交回复
热议问题