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:

Sebastian Probst Eide wrote:

######################### #### 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

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]

Aha! That explains it! Thank you very much!