# Strange behaviour with before\_filter and instance variables

**URL:** <https://www.ruby-forum.com/t/strange-behaviour-with-before-filter-and-instance-variables/70666>\
**Category:** Rails\
**Created:** [October 1, 2006, 7:19pm UTC](https://www.ruby-forum.com/t/strange-behaviour-with-before-filter-and-instance-variables/70666 "2006-10-01T19:19:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sebastian\_Probst\_E](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@Sebastian\_Probst\_E](https://www.ruby-forum.com/u/Sebastian_Probst_E)\
**Post date:** [October 1, 2006, 7:19pm UTC](https://www.ruby-forum.com/t/strange-behaviour-with-before-filter-and-instance-variables/70666/1 "2006-10-01T19:19:19Z")

</div>

Dear message-board users  
I am still learning Ruby on Rails so the answer to the question I am  
posting might be obvious, but I can’t see any logical reason why I am  
getting this strange behaviour, so here goes:

To test what I had learned in the Ajax chapter of Agile Web development  
with Rails I created a simple app which presents the user with a page  
that has three remote links back to the server. Only one of the links  
has a method in the controller. The other two just invoke one view  
each. Since I didn’t want the layout to be rendered for any of the  
three as they were generating output for an ajax-request, and to honour  
the DRY principle at the same time, I created a before\_filter executing  
a method which set rendering of the layout to false when the three  
methods were called. But for some reason instance variables aren’t  
passed to the view when I set the layout to false in the before filter.  
To get the variables passed along to the view I have to set the  
rendering of the layout to false in the method itself. Why is that?  
Does that make any sense?

I’ll include the source code to make it easier to understand what works  
and what doesn’t:

#########################

#########################

#### DOES WORK

#########################

# in this version the before filter is only set for

# the two ajax-calls which don’t have methods

# in the controller but just render views

before\_filter :no\_layout, :only =\> [:link1, :link2]

def navn  
# @resultat is passed to the view  
@resultat = finn\_navn(params[:navn])  
render :layout =\> false  
end

private  
def no\_layout  
render :layout =\> false  
end

#########################

#### DOES NOT WORK

#########################

# in this version the before filter is set for all

# three ajax-calls. :navn is the only one that has a

# method in the controller

before\_filter :no\_layout, :only =\> [:link1, :link2, :navn]

def navn  
# @resultat is NOT passed to the view  
@resultat = finn\_navn(params[:navn])  
end

private  
def no\_layout  
render :layout =\> false  
end

#########################

Thank you.

Best regards  
Sebastian Probst E.

---

<div class="post-metadata">

**Author:** ![Sebastian\_Probst\_E](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@Sebastian\_Probst\_E](https://www.ruby-forum.com/u/Sebastian_Probst_E)\
**Post date:** [October 6, 2006, 11:45pm UTC](https://www.ruby-forum.com/t/strange-behaviour-with-before-filter-and-instance-variables/70666/2 "2006-10-06T23:45:27Z")

</div>

Sebastian Probst E. wrote:

> @resultat = finn\_navn(params[:navn])  
> end
> 
> private  
> def no\_layout  
> render :layout =\> false  
> end

The view template is rendered at the point render is called.  
In the above code the render is happening in a before\_filter,  
before the instance variable is set in navn.

Instead of the before\_filter, write:

layout nil, :only =\> [:link1, :link2, :navn]

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

---

<div class="post-metadata">

**Author:** ![Sebastian\_Probst\_E](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@Sebastian\_Probst\_E](https://www.ruby-forum.com/u/Sebastian_Probst_E)\
**Post date:** [October 7, 2006, 4:34pm UTC](https://www.ruby-forum.com/t/strange-behaviour-with-before-filter-and-instance-variables/70666/3 "2006-10-07T16:34:24Z")

</div>

Mark Reginald J. wrote:

> Sebastian Probst E. wrote:
> 
> > @resultat = finn\_navn(params[:navn])  
> > end
> > 
> > private  
> > def no\_layout  
> > render :layout =\> false  
> > end
> 
> The view template is rendered at the point render is called.  
> In the above code the render is happening in a before\_filter,  
> before the instance variable is set in navn.
> 
> Instead of the before\_filter, write:
> 
> layout nil, :only =\> [:link1, :link2, :navn]
> 
> –  
> We develop, watch us RoR, in numbers too big to ignore.

Also, remember that calling render, redirect\_to, or returning false from  
a before\_filter will halt the filter chain and prevent subsequent  
filters and the action itself from ever executing.

---

<div class="post-metadata">

**Author:** ![Sebastian\_Probst\_E](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@Sebastian\_Probst\_E](https://www.ruby-forum.com/u/Sebastian_Probst_E)\
**Post date:** [November 11, 2006, 9:08am UTC](https://www.ruby-forum.com/t/strange-behaviour-with-before-filter-and-instance-variables/70666/4 "2006-11-11T09:08:47Z")

</div>

Aha! That explains it! Thank you very much!
