THE ZEPINT NETWORK

programmer assist

Ruby Ruby XML Feeds

Ruby Questions Ruby Solutions Ruby Articles

Ruby is a reflective, dynamic, object-oriented programming language. It combines syntax inspired by Perl with Smalltalk-like object-oriented features, and also shares some features with Python, Lisp, Dylan, and CLU. Ruby is a single-pass interpreted language. Its official implementation is free software written in C.

Rails Instance Variables in Controllers?

DiggBlinkRedditDeliciousTechnorati

question by Bejaan | Easy

Hello,

I have the following action in a controller

def edit
@apartment = Apartment.find(params[:id])
render :partial => 'full_edit_apartment', :locals => {:apartment
=> @apartment}
end

This code works just fine if the apartment variable is an instance
variable, but does not work if the apartment variable is not an
instance variable. My question is why?

It doesn't seem like I need an instance variable, since the
full_edit_partial only references a local variable called apartment and
not an instance variable.

- Bejaan

Post reply Subscriptions

Re: Rails Instance Variables in Controllers?

reply by Wasim

It is more to the sequence of how things work

First controller executes then views execute, also being a MVC
framework, they do not share local variables, so it can be instance
variables or global variables which are available to both controller
and views

Regards ,
Wasim Riyazi

Post reply Subscriptions

Re: Rails Instance Variables in Controllers?

reply by Bejaan

Hi,

Thanks for your response, but I still don't understand.

I know that the controller gets executed first and then the view gets
executed. I also know that instance variables in the controller are
passed to the views. You can also pass variables from the controller to
the view via the :locals parameter, which is what I am doing right now.

What I don't understand is why this only works some of the time. Most
of the time that I use the :locals parameter it works just fine. I can
pass information to the view using just local variables. In this
particular case, I can pass the information when I use an instance
variable to store the activerecord object but when I use a local
variable to store the activerecord object, it does not work.

This leads me to believe that I don't understand something about
instance variables or :locals paramater. What am I not understanding
here?

Post reply Subscriptions

Re: Rails Instance Variables in Controllers?

reply by MarkP

Hello,

I'm also confused about this! If the object passed as a local variable exists as instance variable to the controller, then it will be available to the view /as a local variable/ in the view as expected.

If the instance variable is not referenced by anything in the controller, it is not available /even when passed as a local variable/ to the view.

I think this is because the local vars in the controller method are always disposed before the view runs - however if they in reference an instance variable then they are not disposed.

It depends what language you're most familiar with. From my C background I would try to explain what I mean by saying that you're passing the value of a pointer - what that pointer references either gets deleted or not, but the value of that pointer (a memory address) is still the same.

Post reply Subscriptions

Re: Rails Instance Variables in Controllers?

reply by Pond

I've no idea when your question was asked as this forum appears to hide posting dates, which is very unhelpful, so this could be extremely old. However it came up via a Google search on a related topic, so I thought I should post an answer for the sake of archives since the other answers don't seem to be satisfactory.

Rails includes special code which copies instance variables from your controller into a view when that view is rendered following the execution of a controller action. It relies upon the dynamic features of the Ruby language to collect all the instance variables from the controller, create a new instance of a relevant template processor object to build your view, then populate that object with copies of the controller's instance variable values.

When you set "@apartment" in your controller and things work, my guess is that you are also using variable "@apartment" in your view. That is, you refer specifically to an instance variable through using the "@" prefix. If you instead choose to pass a value into a template rendering call using the ":locals" option, then the variable - as the option name suggests - is created within the template object as a local variable itself, *not* an instance variable. So if you're still using "@apartment" inside the view, but nobody is defining this instance variable anymore, then your application will fail. Your view code should be using "apartment" (no "@" prefix) instead in this case.

Summary 1 - Controller:
@apartment = <something>
render :partial => 'full_edit_apartment'

Summary 1 - View:
...use "@apartment", copied from the Controller for your view by Rails.

Summary 1 - Controller:
render :partial => 'full_edit_apartment', :locals => { :apartment => <something> }

Summary 1 - View:
...use "apartment", set by the use of ":locals" in the call to "render" in the controller.

Post reply Subscriptions

Got a Ruby Question?

Just Sign Up and ask the top Ruby experts!

Search via Google

User Login

Email Address

Password

Ruby Experts

Rank Expert Points
#1 Srirangan 1500
This a list of the Top Ruby experts, how many points do you have?

Leading Experts

Rank Expert Points
#1 frankzzsword 4650
#2 Bejaan 2950
#3 csfreak 1100
#4 Anurag 700
#5 keyvez 700
#6 nnarasimha 600
#7 Nakata 600
#8 martinig 600
#9 mastercomputers 550
#10 Abhishek Chatterjee 250
#11 Abels 250
#12 Huntress 150
#13 Adkron 150
#14 Yogesh 100
#15 lexxwern 100
This is a list of overall best performing experts, how many points do you have?