Frederick C. wrote:
On May 10, 6:35ï¿½am, 7stud – email@example.com wrote:
random_name can be accessed in the partial _random_name.html.erb. ï¿½In
addition, my tests show that @var can be accessed in
_random_name.html.erb as well. ï¿½I’m not sure why you would want to stuff
the value of @var into another variable when you can just access @var
directly. ï¿½Switching the names seems confusing to me.
because depending on the state of local variables makes your partial
more dependant on its environment. For example if the random_name
partial always used @var you couldn’t use it to render a collection or
if the thing that you wanted to render was @something.something_else.
Personally I mostly equate this to ‘why use method arguments when you
could just use global variables?’
Ah. I see.
Perhaps you should look at the whole of DHH wrote. It is perhaps
something like: render :partial => ‘test’ or has a :test => @var
clause to give the test variable.
This is what he wrote:
The :object parameter to render takes an object that is assigned to a
local variable with the same name as the partial. So, in the layout we
could call this:
<%= render(:partial => “cart”, :object => @cart) %>
and in the _cart.html.erb template, we can refer to the cart via the
p. 120, AWDWR(3rd)
My tests show that the :partial name can be anything, e.g.
“forrest_gump”, and then inside the file _forrest_gump.html.erb, a
variable named forrest_gump will be available and it will be assigned
whatever value is specified for :object.
In addition, my tests show that all the @variables that are set in the
controller are available in the partial file no matter what I write in
the render() statement. Therefore, a statement such as this:
For example, render :partial => ‘foo’ passes the instance variable
named @foo to the partial where I can then say: for f in @foo.
does not suddenly make @foo available in the partial. @foo is also
available in the partial already. For instance, if I write:
render :partial => “red”
I can still access @foo in the file _red.html.erb.
The code you posted doesn’t contain any variable named test, otherwise
you wouldn’t get the error in the first place.
I explained why I got the error. It was because I ended up with two
conflicting render statements, one in the layout and one in the index
render(:partial => “some_name”, :object => @var)
render(:partial => “some_name”, :collection => @var)
Apparently, that confused rails. The error had nothing to do with the
fact that ‘test’ wasn’t previously defined somewhere in my code.
If you don’t want help - why post here?
Don’t get offended just because a beginner can’t confirm how you say
rails works. I don’t blindly accept what someone says. I test things
out to see how they work for myself. If my test results don’t confirm
what I’ve been told, then I ask questions.
Variable names are special - they don’t just appear and have to be created.
Is it so hard to believe that rails could create a variable when given a
string, like here:
partial => “forrest_gump”
I can do it:
a = A.new
str = “forrest_gump”
val = [1, 2, 3]
class << a
[1, 2, 3]
I don’t know if things work differently in rails 2.3.2, which is what I