0 Replies - 825 Views - Last Post: 19 January 2012 - 02:12 PM Rate Topic: -----

#1 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

Rails - Should we send hashes into partials as a parameter? #IfSoHow

Posted 19 January 2012 - 02:12 PM

So I've got a happy little html.erb file that I call with ajax, and it returns a submenu. It looks like this right now:


(submenu1)
<% @functions = { "item1" => "this is the first item", "item2" => "this is second item" } %>

<div class="submenu">
	<div id="module-name">Point of Sale</div>
	
	<table id="submenu-table" RULES=COLS FRAME=BOX> <!-- $("#submenu-table tr:nth-child(2) td span") -->
		<tr style="height:0;">
			<th width="20"></th>
			<th width="370"></th>
		</tr>
		
		
		<% @functions.each do |key, value| %>
			<tr>
				<td><span class="ylw"><%= key %></span></td>
				<td><span><%= value %></span></td>
			</tr>
		<% end %>
		
		
	</table>
</div>




There will be 10 identical submenus so I'm thinking of ways to refactor this portion:

(irb snippet to refactor)
		<% @functions.each do |key, value| %>
			<tr>
				<td><span class="ylw"><%= key %></span></td>
				<td><span><%= value %></span></td>
			</tr>
		<% end %>




What I've come up with seems a little iffy, because I can't figure out how to explicitly send in that hash instance variable as a parameter.


(refactored irb snip)
<%= render :partial => 'spit_out_submenu_items' %>




(_spit_out_submenu_items.html.erb)
<% @functions.each do |key, value| %>
	<tr>
		<td><span class="ylw"><%= key %></span></td>
		<td><span><%= value %></span></td>
	</tr>
<% end %>




Edit:

I went back and tried something again and it worked for passing in the hash (I must have gotten confused with the hash declarations, they still look strange to me).

(refactored snippet)
<%= render :partial => 'spit_out_submenu_items', :locals => { :functions => @functions }%>



(partial)
<% functions.each do |key, value| %>
	<tr>
		<td><span class="ylw"><%= key %></span></td>
		<td><span><a href="#" class="submenu-a"><%= value %> </a></span></td>
	</tr>
<% end %>



That's a pretty standard practice for this, right?

This post has been edited by NotarySojac: 19 January 2012 - 04:19 PM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1