Strange behaviour with before_filter and instance variables

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.

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.

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.

Aha! That explains it! Thank you very much!