Access protected flash in block of before_filter?

Hi all

I’d like to put a block after a before_filter instead of calling a
separate method because I think it’s looking cooler. :wink:

before_filter :only => [:new, :create] do |controller|
flash[:notice] = “That is not allowed!.”
redirect_to :referer
end

Sadly this doesn’t work:

ndefined local variable or method `flash’ for
SchmuckSeitenController:Class

I tried it with controller.flash, but then Rails tells me it is
protected.
Is there a way to access flash? :slight_smile:

Thanks
Josh

Joshua M. wrote:

ndefined local variable or method `flash’ for
SchmuckSeitenController:Class

I tried it with controller.flash, but then Rails tells me it is
protected.
Is there a way to access flash? :slight_smile:

You can wrap your filter body in a controller.instance_eval block.

But this is really a job for verify:
http://api.rubyonrails.org/classes/ActionController/Verification/ClassMethods.html


We develop, watch us RoR, in numbers too big to ignore.

But this is really a job for verify:
http://api.rubyonrails.org/classes/ActionController/Verification/ClassMethods.html


We develop, watch us RoR, in numbers too big to ignore.

Thanks, mate. I tried it the following way, but it doesn’t work:

verify :only => [:new, :create],
:add_flash => {:notice => ‘Creation is deactivated.’},
:redirect_to => :back

I want to prevent the user from creating a new object, but maybe later I
will allow it to the user. So I just want to make sure he can’t access
the new and create methods.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs