Topic: Using partials with parameters?

Hi

I currently have a view that uses tabs to "filter" based on a single column of my model. If we took the Product model as an example, each tab would be for a certain category. I also made an action in my controller to get all the objects for one specific category.

I then made a partial that renders a table with one row for each of the products.

How do I stitch the pieces together, in order to actually render these partials in my view? Because I use these tabs, each of the categories is in fact still on one page: <div id="category1">...</div><div id="category2">...</div> and so on

- the view is current index_by_category.html.erb
- the controller action in products_controller is also index_by_category
- the partial i did not yet create, but it's the same one I already use to show ALL items except that it obviously doesn't have a category column


Thank you!

Re: Using partials with parameters?

Just pass the variables into the partial in the render statement
e.g. for Rails 3.x
_a_partial.html.erb

<h2>My partial</h2
<%= a_variable %>

index.html.erb

  <h1>Index</h1>
  <%= render "a_partial", :a_variable => "A value", another_var => 'something_else %>

when index is rendered it would produce something like this in the browser

Index
My Partial
A value

The old Rails 2 way is to use :locals hash
index.html.erb

  <h1>Index</h1>
  <%= render "a_partial", :locals => {:a_variable => "A value} %>
What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)