Forum: Ruby on Rails special case rendering from filter

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.
E1af86717137575d94bed5d562c6f4f1?d=identicon&s=25 Zac Zheng (zaczheng)
on 2009-04-08 20:59
I have a special case(does not normally occur) render in a filter.
Because its in a filter, I can't end execution with a 'return' which
would work otherwise. Therefore I get this error:

Can only render or redirect once per action

My code in gist:

class MyController < ApplicationController
  before_filter :authenticate

  def priviledged_action
    redirect_to :action => "vip_room"
  end

protected
  def authenticate
    if params[:password] != "pass"
      render :status => 403,  :text => "Access denied"
      # Things I have tried to end execution here:
      # 'return' does end execution as simply returns filter
      # 'exit' causes crash
      # I can't think of a way to use catch/throw
    end
  end
end

Can anyone please advise?
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-04-09 00:58
(Received via mailing list)
On Apr 8, 7:59 pm, Zac Zheng <rails-mailing-l...@andreas-s.net> wrote:
> I have a special case(does not normally occur) render in a filter.
> Because its in a filter, I can't end execution with a 'return' which
> would work otherwise. Therefore I get this error:
>
> Can only render or redirect once per action
>
what version of rails are you using ? In rails 2 and above rendering
from a filter should cause the filter chain to halt. in previous
versions you had to return false from your filter.

Fred
E1af86717137575d94bed5d562c6f4f1?d=identicon&s=25 Zac Zheng (zaczheng)
on 2009-04-10 16:00
Frederick Cheung wrote:
> what version of rails are you using ? In rails 2 and above rendering
> from a filter should cause the filter chain to halt. in previous
> versions you had to return false from your filter.

You're right! My semi pseudocode oversimplified my actual code. There's
a logic that prevents false being returned from the filter.

Thanks mate
This topic is locked and can not be replied to.