Topic: Looking for goosebumps!

The below code makes a bunch of table row entries -- it is found in a helper.  I hate using the line variable... is there are more magical way to short-hand this that gives you goose-bumps?

I'm sure I'll be doing similar things to this in the future, there has got to be a "more ruby way" -- you know somethig that when you look at it, makes you happy inside?

    RoleType.find(:all).map { |role| 
   
      line  = content_tag("td", h(role.name))
      line += content_tag("td", check_box_tag("user[role_type_ids][]",
                                              role.id,
                                              @user.role_types.include?(role)))
      line += content_tag("td", h(role.description))
         
      content_tag("tr", line)
     
   }.join

Maybe something with yield, and code blocks?  Oh I don't know -- that's why I'm asking! smile

Re: Looking for goosebumps!

There was an excellent blog post a while ago about extending content_tag to support blocks. Unfortunately I can't find it anymore. If you want the code to do this I may be able to find it tomorrow. With that you could do:

RoleType.find(:all).map { |role|
  content_tag("tr") do
    content_tag("td", h(role.name)) +
    content_tag("td", check_box_tag("user[role_type_ids][]",
                                            role.id,
                                            @user.role_types.include?(role))) +
    content_tag("td", h(role.description))
  end
}.join

Concatenating like this isn't the prettiest, you may still want to stick with the line variable.

A block with content_tag is useful in many situations, but in this case I think a partial is a better solution:

# in _role.rhtml
<tr>
  <td><%= h(role.name) %></td>
  <td><%= check_box_tag("user[role_type_ids][]", role.id, @user.role_types.include?(role)) %></td>
  <td><%= h(role.description) %></td>
</tr>

You can then call it from a template or helper like this:

render :partial => 'role', :collection => RoleType.find(:all)

Railscasts - Free Ruby on Rails Screencasts

Re: Looking for goosebumps!

Thanks ... I could use a partial like you said (I use partials a lot), but I think I'm not sure I want to use a partial for every little dinky thing.  Or maybe I should.  Hmm...