On Feb 11, 2011, at 6:05 PM, Bilal Ahmed wrote:
> I have a repeat in show-page
>
> <repeat with="&@comments"/><a/>
>
> It works
I'm really curious as to what "works" means in this context - the code written
above won't output anything but a single link, since you've closed the repeat
tag without a body. Did you perhaps mean:
<repeat with="&@comments"><a/></repeat>
?
> but as soon as I add group by
>
> <repeat with="&@comments.group_by(&:job_post)"/><a/>
>
> it fails with the error:
>
>
> NoMethodError in Job_posts#show
>
> Showing /Users/bilal/rails_projects/interns_partners/app/views/
> job_posts/show.dryml where line #8 raised:
>
> You have a nil object when you didn't expect it!
> You might have expected an instance of Array.
> The error occurred while evaluating nil.group_by
> Extracted source (around line #8):
>
> 5:
> 6: <collection:>
> 7:
> 8: <repeat
> with="&@comments.group_by(&:job_post_id)"/><a/><br/>
It looks like you're wanting to iterate over all the comments on a particular
post (based on the view being rendered). This raises a couple questions:
- will the group_by actually do anything? :job_post_id looks an awful lot like
the key might connect a comment to a post...
- more importantly, the <collection:> parameter means you're *already* inside a
repeat. So this might do something more useful:
<collection:>
<!-- in here, 'this' refers to a comment -->
<a /><br />
</collection:>
which, assuming the comments are set as 'children' (see the stuff on view
hints) will produce a link to each comment.
If that's not what you want, the 'replace' flag will pull out the entire
collection tag and replace it with the new markup:
<collection: replace>
<!-- right here, 'this' still refers to the job post -->
<repeat with="&@some_other_array_entirely">
<!-- now 'this' refers to each element of @some_other_array -->
</repeat>
</collection:>
- one other note: the default Hobo show action doesn't load related models into
separate instance variables, so comments attached to a job post are available
as this.comments, and @comments won't be set at all. Ignore this if you're
defining your own action and setting @comments...
- if all else fails and you need to guard against nils, you can use _? to avoid
NoMethodErrors:
<repeat with="&@comments._?.group_by(&:job_post_id)">
...some code...
</repeat>
Hope this helps!
--Matt Jones
--
You received this message because you are subscribed to the Google Groups "Hobo
Users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/hobousers?hl=en.