Topic: Want some input on locals to a partial

I have a partial in a shared folder. A lot of the stuff in the partial is reused in a few places on my rails site. Some uses require to show certain things, some don't, etc. This partial is used to show a used book or collection of used books. When I call this partial on the user show action, I hide the user name being shown. I also show the destroy link. On the index page where it shows some used books, I show the user name and the destroy link (provided it's the owner of the book of course). There's another local variable I need to add, and i am starting to think it's becoming combersome. I am looking for some input on how I can improve this.

From the user show view:

<%= render :partial => 'shared/used_book', :collection => @used_books, :locals => { :user => false, :destroy => true } %>

The partial:

<% book = used_book.book %>
<div class="bookContainerGrid bookContainerFloat">
  <ul>
    <% unless book.medium_image_path.blank? -%>
      <li style="margin-right:5px"><%= link_to image_tag(book.medium_image_path), used_book_path(used_book) %></li>
    <% end -%>
    <li style="font-size:16px"><b>Price:</b> <%= number_to_currency(used_book.price) %></li>
    <% unless is_owner?(session[:user_id], used_book.user.id) %>
      <li><%= button_to "Buy Now!", buy_now_path(used_book) %> </li>
    <% end -%>
    <li><b>Title:</b> <%= truncate(book.title, :length => 25) %></li>
    <li><b>Author:</b> <%= truncate(book.author, :length => 15) %></li>
    <% if user -%>
      <li>sold by: <%= sold_by used_book %></li>
    <% end -%>
    <% if destroy && is_owner_or_admin?(current_user, used_book.user_id) -%>
      <li><%= link_to "Remove posting", used_book, :confirm => 'Are you sure?', :method => :delete %> </li>
    <% end -%>
  </ul>
</div>
<% used_book_counter = used_book_counter || nil %>
<% unless used_book_counter.nil? -%>
  <%= '<div class="clear"></div>'.html_safe if (used_book_counter + 1) % 4 == 0 %>
<% end -%>

Thanks.

Re: Want some input on locals to a partial

One simple way to make life easier is to pass a hash with your local variables in it, rather than the variables themselves, eg

:locals => {:options => {:destroy => true} }

Now your code can say eg
if options[:user]

and you don't have to worry about defining user in all the times you call the partial (to avoid "unknown method or variable" errors): if it's not present then it's nil, and that's the same as setting it to false.

Last edited by Max Williams (2010-06-30 11:19:59)

###########################################
#If i've helped you then please recommend me at Working With Rails:
#http://www.workingwithrails.com/person/ … i-williams

Re: Want some input on locals to a partial

Not a bad idea, generally how a lot of the framework helpers are done. Thanks max smile.

Re: Want some input on locals to a partial

no problem.  I've got in this situation before and done the above and thought it was a bit hacky.  I never really thought of a cleaner way of doing it though.  Maybe it's better to partial off all off the shared bits so you end up with two (or more) distinct files which contain only the differences between the pages, and call lots of code snippets from other partials.  Seems like a lot of hassle though smile

###########################################
#If i've helped you then please recommend me at Working With Rails:
#http://www.workingwithrails.com/person/ … i-williams