Ruby on Rails Thursday, February 19, 2015

I actually ended up doing this which worked:

    <% last_artist ||= nil %>
  <table id="artist">      <tr>        <th>Artist</th>        <th></th>        <th>Album</th>     </tr>  <% @pin_albums.each do |pin| %>      <% if last_artist != pin.artist %>      <tr>        <td><%= pin.artist %></td>        <td><%= link_to image_tag(pin.image), pin %></td>        <td><%= link_to pin.album, copy_pin_path(pin) %></td>      </tr>      <% else %>      <tr>        <td></td>        <td><%= link_to image_tag(pin.image), pin %></td>        <td><%= link_to pin.album, copy_pin_path(pin) %></td>      </tr>    <% end %>         <% last_artist = pin.artist %>       <% end %>


On Thu, Feb 19, 2015 at 2:45 PM, Hassan Schroeder <hassan.schroeder@gmail.com> wrote:
On Thu, Feb 19, 2015 at 5:57 AM, TTambe <tambe257@gmail.com> wrote:
> Someone suggested that I use local variables like this:
>
> <% @pin_albums.each do |pin| %>
>   <%= (last_artist ||= nil) != pin.artist ? (last_artist = pin.artist) : ''
> %> |
>   <%= link_to pin.album, copy_pin_path(pin) %>
>   <br/>
> <% end %>

Hint: "scope"

2.1.0 (main):0 > 3.times{ puts (val ||= 0); val += 1 }
0
0
0
=> 3
2.1.0 (main):0 > 3.times{ puts (@val ||= 0); @val += 1 }
0
1
2
=> 3

HTH,
--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
http://about.me/hassanschroeder
twitter: @hassan
Consulting Availability : Silicon Valley or remote

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CACmC4yCx5jJcSisuZV08jHsCC0hsgPpY8xhpVtnX8e65vL-KiA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAEdg5qT8oxhDMcQUbv93%3D_KC1nzsAjFQeBetGR3-0R_cndgciQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment