How do I stop controller execution after using redirect_to? (Using Rails)

35,523

Solution 1

You probably want to use filters.

If you call your check_date as a before_filter in the controller, the fact that it rendered or redirected will prevent the controller from ever calling the action method. It ends there and then.

Solution 2

You can also do:

return redirect_to :action => 'index'

and

return redirect_to :action => 'month_index', 
  :year => Date.today.year, 
  :month => Date.today.month,
  :type => params[:type]

since it looks nicer than putting return on its own line (IMHO).

Solution 3

You can throw in

return false

wherever you want the code execution in your action to stop

Solution 4

redirect_to just tells rails what to render when it finishes. Rails will get confused if you add other render or redirect_to directives after the one you really want, so just return from the controller after the redirect_to - it's the 'normal' rails way to do things.

Solution 5

I think the OP is confused about the function of redirect_to.

redirect_to will redirect at the end of the action. However, the rest of the controller function will execute as usual. All you have to do (as posted by other people) is to include a return, as you should any other function call.

Share:
35,523

Related videos on Youtube

Hamish Downer
Author by

Hamish Downer

Unless I explicitly state otherwise in a post, all my original contributions to StackOverflow and ServerFault are placed into the public domain. If this is not legally possible, then anyone receiving a copy of them by any means is granted a non-exclusive perpetual license to use, distribute and modify them, and to distribute modifications, under any license or none, with or without attribution to me. Please note that this license applies only to my original contributions.

Updated on November 13, 2020

Comments

  • Hamish Downer
    Hamish Downer about 3 years

    I have a controller with multiple actions that take :year and :month as attributes from the URL. I have made a private method check_date to check the date is valid and check the date is not in the future.

    def check_date(year, month)
      if month < 1 || month > 12 || year < 2000
        flash[:notice] = I18n.t 'archive.invalid_date'
        redirect_to :action => 'index'
      elsif year > Date.today.year || (year == Date.today.year && month > Date.today.month)
        flash[:notice] = I18n.t 'archive.no_future'
        redirect_to :action => 'month_index', 
          :year => Date.today.year, 
          :month => Date.today.month,
          :type => params[:type]
      end
    end
    

    Is there a rails way of ending controller execution after the redirect_to?

    Ways I can think of are either to throw an exception after the redirect_to or to return a value from check_date and check it in each action that calls it - something like

    def month_index 
      year = params[:year].to_i
      month = params[:month].to_i
      if !check_date(year, month)
        return
      ...
    end
    

    But I wonder if there is some nice rails way of doing this. I was half hoping that having called redirect_to rails would recognise I wanted to stop, but that doesn't seem to happen.

  • iphone007
    iphone007 almost 9 years
    This may solve the asker's problem but I came here expecting a solution like vrish88's.
  • Rigo
    Rigo almost 7 years
    Or redirect_to :action => 'index' and return
  • Matrix
    Matrix over 4 years
    I do but "Render and/or redirect were called multiple times in this action"
  • Slipp D. Thompson
    Slipp D. Thompson over 4 years
    @Matrix Unfortunate. It looks like this no longer works in Rails 5.
  • collimarco
    collimarco about 4 years
    @Rigo I don't think it is safe your idea, because if redirect_to returns a falsy value then your return will be ignored!

Related