Ruby on Rails
Thursday, February 19, 2015
Yes, the above was a paste from my actual code. I added the parentheses, but there was no change. I added <%= last_artist %> and the output was just each artist again. So it seems that there is something wrong with teh portion that checks last_artist against pin.artist.
On Thu, Feb 19, 2015 at 9:13 AM, Colin Law <clanlaw@gmail.com> wrote:
On 19 February 2015 at 13:57, 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 %>
>
> This gives me the same result as my original code:
>
> The Beatles | Let It Be
> The Beatles | Abbey Road
> Bob Dylan | Blood On The Tracks
> Bob Dylan | Highway 61 Revisited
That looks as if it should work. Have you posted a direct copy/paste
from your code?
It assigns nil to last_artist unless last_artist already has a value,
then compares that to pin.artist. If not the same (so a new artist)
it assigns pin.artist to last_artist and displays that, if the the
same then it displays an empty string.
I wonder whether the precedence is wrong. If the above is a copy/paste
from your code then try
((last_artist ||= nil) != pin.artist) ? (last_artist = pin.artist) : ''
Note the extra parentheses
If that still doesn't work add a column on the end of the line showing
last_artist and see if it is tracking correctly.
Colin
> https://groups.google.com/d/msgid/rubyonrails-talk/CAEdg5qTkpQs%3DsbVg6QgS8fRf8JOQop_ryMJA9wb6epQfKjf%3DMg%40mail.gmail.com.
>
> I'm not well versed on writing local variables in RoR, so I'm not sure that
> I'm analyzing this correctly, but I don't see where last_artist is
> referencing the last iteration of the loop. I was thinking of trying to use
> @pin_albums.each.with_index and then somehow comparing pin.artist to
> pin.artist - 1
>
> Any thoughts from everyone would be appreciated.
> Thanks!
>
> On Tue, Feb 17, 2015 at 11:07 AM, Colin Law <clanlaw@gmail.com> wrote:
>>
>> On 17 February 2015 at 15:39, Tony Tambe <tambe257@gmail.com> wrote:
>> > In my music review app the Pins model has attributes of Pin.artist and
>> > Pin.album. I'm trying to list each artist reviewed on the site and which
>> > albums of theirs have been reviewed. Below is what I have so far, but I
>> > want
>> > to do it without repeating the artist name.
>> >
>> > Controller:
>> >
>> > @pin_albums = Pin.group(:album).order('artist')
>> >
>> > View:
>> >
>> > <% @pin_albums.each do |pin| %>
>> > <%= pin.artist %> |
>> > <%= link_to pin.album, copy_pin_path(pin) %>
>> > <br/>
>> > <% end %>
>> >
>> > This lists them like this:
>> >
>> > The Beatles | Let It Be The Beatles | Abbey Road Bob Dylan | Blood On
>> > The
>> > Tracks Bob Dylan | Highway 61 Revisited
>> >
>> > I want to list them like so:
>> >
>> > The Beatles | Let It Be
>> > | Abbey Road
>> > Bob Dylan | Blood On The Tracks
>> > | Highway 61 Revisited
>>
>> Leave the artist cell blank if the artist this time is the same as the
>> artist last time.
>>
>> Colin
>>
>> --
>> 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/CAL%3D0gLsQnnC32Xru-A6P1_Tr6SeL2ex6a%3DNCUOxV9Yrjr5%2BtJQ%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
>
> 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/CAL%3D0gLs2yYaqwmrCV4sYXYDnniaWK03cHBJGGhwimXava69Tzg%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/CAEdg5qRM4%2Bboh1VXx10jDb-CGvXGpEqWytkkz2rHHkop1D%2BgLQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment