Forum: Ruby on Rails Render and/or redirect were called multiple times in this action

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
445e1d91a558e823a112aed8a8fa4808?d=identicon&s=25 BarefootSanders (Guest)
on 2008-12-04 09:58
(Received via mailing list)
I'm trying to add an application enabled/disabled flag in my
application.. This is my code.

[code=]  # application.rb
  ...
  before_filter :check_enabled_flag

  private

  def check_enabled_flag
    application_flag = ApplicationFlag.find(:first)

    if application_flag.application_enabled
      redirect_to application_disabled_path and return
    end
  end[/code]
and for some reason i keep getting this error

[code=]Render and/or redirect were called multiple times in this
action. Please note that you may only call render OR redirect, and at
most once per action. Also note that neither redirect nor render
terminate execution of the action, so if you want to exit an action
after redirecting, you need to do something like "redirect_to(...) and
return".[/code]
the database is set up correctly and it works when the site is
enabled.  when its disabled i get the error above.  any suggestions as
to how to fix this?

thanks.
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-12-04 11:02
(Received via mailing list)
On 3 Dec 2008, at 23:08, BarefootSanders wrote:

>  def check_enabled_flag
>    application_flag = ApplicationFlag.find(:first)
>
>    if application_flag.application_enabled
is this typo ? surely you only redirect when the application is not
enabled.

What version of rails is your app running ? For rails >= 2.0 then
redirecting from a filter halts execution, but in previous versions
you had to return false from the filter to achieve that.

Fred
519d971b4225781ff523082fff137f31?d=identicon&s=25 Mahmoud Said (Guest)
on 2008-12-04 14:11
(Received via mailing list)
It seems the same filter is being executed for the redirected action
too...
so u r in infinite loop

make sure the filter is skipped for the action you are redirecting to.
use
this

before_filter :check_enabled_flag, :except=>[:disabled]




On Thu, Dec 4, 2008 at 1:08 AM, BarefootSanders <mgolds02@gmail.com>
wrote:

>  def check_enabled_flag
> most once per action. Also note that neither redirect nor render
>    end
>
> >
>


--
Mahmoud Said
Software Developer
blog.modsaid.com
www.eSpace.com.eg
This topic is locked and can not be replied to.