views:

57

answers:

2

In my .html.erb file, I have a lot of lines of information to display. I am accomplishing this with a simple: (using blueprint css)

<% for event in @user.events %>
<div class="span-5 border">Descriptor:</div> <div class="span-5 last"><%=h event.foo%></div><br/>
<% end %>

How can I make it so that I can call some function that would render the information part and I would pass it just the Descriptor (a string literal) and the method for event ("foo" in this case).

I'm new to rails and I think this could be accomplished using a partial render, but I'm not entirely sure.

Thanks

+1  A: 
render :partial => 'event', :collection => @user.events, :locals => {:descriptor => 'descriptor', :method => 'foo'}

If you're using a recent version of rails this shorthand version will also work:

render @user.events, :locals => {:descriptor => 'descriptor', :method => 'foo'}

In your partial

<div class="span-5 border"><%= descriptor -%></div> <div class="span-5 last"><%=h event.send(method) %></div><br/>
mark
What would the _event.erb look like?
Reti
Hi Reti. Sorry for incomplete description, I've edited answer. This should work but let me know if not.
mark
Thanks for the help so far. I put in `<%= render @user.events, :locals => {:descriptor => 'descriptor', :method => 'foo'} %>`under the `<% for event in @user.events %>` and the partial you said in the correct place, but now it's saying 'descriptor' is undefined. I also tried the other render you provided, but it says that event is undefined. What's going on?
Reti
In the second case it's not finding the partial file which probably means it's not in the same directory or named differently - it should be named with a prefix underscore - "event.html.erb" . The first case, you should remove the for loop; the render @user.events it to be an array and iterates over it. As to why descriptor is undefined I'm not sure but try removing the loop and see what happens. I assume descriptor is just a string being passed as in the example?
mark
"_event.html.erb" rather... Also the rails guide is really good - http://guides.rubyonrails.org/layouts_and_rendering.html
mark
A: 

You can use partials. Its advantage that it makes code DRY.Also you can replace the particular div by Ajax callback. Save the partials with prefix '_' on file name like _test.rhtml

render :partial => 'test', :layout => false