Rails Tip : Passing variables to "render :partial"
Inspired by John Topley's post, "Rails Tips #1: Local Documentation", I want to drop a "bite-size" Ruby on Rails tip.
You can pass variables into a partial's local scope like so:
<%= render :partial => 'some_partial',
:locals => {:some_variable => "somevalue",
:some_other_variable => some_other_variable} -%>
It took me a while to come across this style of coding. I think I stumbled across it on the Ruby on Rails, "Ruby Forum". Before I found out about this I was just putting everything into instance variables so all my .rhtml templates could access the values. (When you've got the "@" symbol before the variable name, it's an instance variable.) I found it difficult to keep track of what was going on in my code when everything was an instance variable, and I much prefer passing variables directly into my partial's.
You can pass variables into a partial's local scope like so:
<%= render :partial => 'some_partial',
:locals => {:some_variable => "somevalue",
:some_other_variable => some_other_variable} -%>
It took me a while to come across this style of coding. I think I stumbled across it on the Ruby on Rails, "Ruby Forum". Before I found out about this I was just putting everything into instance variables so all my .rhtml templates could access the values. (When you've got the "@" symbol before the variable name, it's an instance variable.) I found it difficult to keep track of what was going on in my code when everything was an instance variable, and I much prefer passing variables directly into my partial's.


7 Comments:
Thanks for the link, Michael! :-)
On the subject of partials, there's something that I've been struggling to work out how to do.
I have an unordered list of links for a menu whereby the active list item has an ID of "active". How would I put this menu code in a partial and parameterise it so that each page using the partial could specify which menu item should be active? Any ideas?
Go easy on me, I'm just getting my feet wet with Rails!
Hi John,
I'll take a stab at it:
<%= render :partial => 'common/_menu',
:locals => {:active_menu_item => 'home'} -%>
Then in the partial you'd just reference the local variable, "active_menu_item" to know which item is "active".
But, I'm not personally a big fan of including my menu code in every page of my app. Have you worked with Rails' "layouts"? I'd centralize all the logic for displaying the menu there, and maybe have your controller (or the methods in your controller) flag which menu item you're on.
Does that make sense? I could expand on it a bit more if you need me to.
Thanks Michael. I think the problem is that only the active list item should have an ID on it, none of the other items should (it's basically this: http://css.maxdesign.com.au/listamatic/horizontal09.htm)
I can't see any way to do it without a lot of branching logic in the menu to conditionally add and remove IDs. It may not be worth it...
Yeah, most HTML menus end up with that "if active do something a bit different" code in them.
Or you can have a look at link_to_unless_current which would disable your menu link if you are viewing it on the current page.
Thanks for the tip anonymous, I wasn't aware of that function.
I'm not sure if it would work for all menu's though. Sometimes you've got more than one URI under a single tab or menu item.
Also thanks to John & Anonymous, I also wasn't aware of that function which also led me to the function current_page? which I've now just used to activate the appropriate item in my unordered list menu.
Post a Comment
<< Home