can't get a simple partial to work !#$@#!$!!

views/store/index.html.erb:

<%= render(:partial => "layouts/test", :object => @array) %>

views/layouts/_test.html.erb:

<% for num in test %> <div><%= num %></div> <% end %>

To achieve this I use the syntax

<%= render :partial => ‘cart’, :locals => {:mycart = @cart} %>

Then use the variable mycart in the partial

Colin

Colin Law wrote:

To achieve this I use the syntax

<%= render :partial => 'cart', :locals => {:mycart = @cart} %>

I think that is supposed to be:

<%= render :partial => 'cart', :locals => {:mycart => @cart} %>

Then use the variable mycart in the partial

and that produces the same error:

hi,

try this:

7stud -- wrote:

<%= render :partial => 'cart', :locals => {:mycart => @cart} %>

  <%= render :partial => 'cart', :collection => @cart %>

Then use the variable mycart in the partial

views/store/_cart.html.erb

Colin Law wrote:

To achieve this I use the syntax

<%= render :partial => ‘cart’, :locals => {:mycart = @cart} %>

I think that is supposed to be:

<%= render :partial => ‘cart’, :locals => {:mycart => @cart} %>

True, this (as corrected) works fine for me on rails 2.2.2. Which version are you using?

if you go to /store that's just the index action so add_to_cart is never called and @cart is not set.

Fred

Frederick Cheung wrote:

� � @cart = [1, 2, 3] � end end

if you go to /store that's just the index action

Yes. I realize that>

so add_to_cart is never called and @cart is not set.

Ok, but if I go to /store/index why does the add_to_cart view ever come into play? @ variables in other actions are not set when /store/index executes, so why doesn't rails throw errors when their views reference the @ variables set in their actions?

7stud -- wrote:

Frederick Cheung wrote:

� � @cart = [1, 2, 3] � end end

if you go to /store that's just the index action

Yes. I realize that>

so add_to_cart is never called and @cart is not set.

Ok, but if I go to /store/index why does the add_to_cart view ever come into play? @ variables in other actions are not set when /store/index executes, so why doesn't rails throw errors when their views reference the @ variables set in their actions?

Ai yi yi. I'm putting the partial in the store *layout* so all the store views use it--including the index view.

Thanks.

Maybe you should send the var through each page, you need to store it and get it by method. store it: :locals => {:var = @a}

get it: var = local_assigns[:var]

When you render a partial you automagically get an object with the same name available in the partial

For example, render :partial => 'foo' passes the instance variable named @foo to the partial where I can then say: for f in @foo.

In your first post you iterate over 'test' but that variable isn't created anywhere.

Using: for a in @array might work providing that the partial is part of that controller though. Or rename @array to @test.

I don't know how many times I have stopped and looked for the painfully obvious - like with RESTful resources - the key is to make that part of the trouble shooting.

AGoofin wrote:

When you render a partial you automagically get an object with the same name available in the partial

For example, render :partial => 'foo' passes the instance variable named @foo to the partial where I can then say: for f in @foo.

How do you know that @foo isn't already available to all the views and that writing render(:partial => foo) doesn't have any effect whatsoever on whether @foo is available in the partial?

In your first post you iterate over 'test' but that variable isn't created anywhere.

Sure it is. Try it. I'm just a beginner but my Dave Heinemeier Hansson book says the variable test gets created by the render() statement, and my tests confirm that's the way it works. More generally, when I write

<% render(:partial => "random_name", :object => @var) %>

and @var was assigned a value in the controller, then I find that the value of @var gets assigned to the variable "random name", and 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?'

Fred

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.

The code you posted doesn't contain any variable named test, otherwise you wouldn't get the error in the first place.

If you want the code to work, replace 'test' in the index view with '@array'

7stud -- wrote: Corrections/clarification: