Render collection with a non-standard partial?

19 views
Skip to first unread message

Walter Lee Davis

unread,
Apr 21, 2015, 10:57:57 AM4/21/15
to phil...@googlegroups.com
I have a collection, and I want to render it within lightboxes#show like this:

<%= render @slides %>

Works perfectly, as long as views/slides/_slide.html.erb exists, and it does.

Now I want to leverage that same simple construction in my edit page, where I also have a collection of slides, but I want the partial to have drag widgets and delete links, so I made views/slides/_edit.html.erb. But no matter what I tried, I could not find a one-line way to render @slides with that different partial. I ended up writing the loop out long-hand, in three lines, like an animal. What am I forgetting here? I could swear there's a way around this.

Thanks,

Walter

Jason Garber

unread,
Apr 21, 2015, 2:17:44 PM4/21/15
to phil...@googlegroups.com, phil...@googlegroups.com
You're using the shorthand for 



--
You received this message because you are subscribed to the Google Groups "Philly.rb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to phillyrb+u...@googlegroups.com.
To post to this group, send email to phil...@googlegroups.com.
Visit this group at http://groups.google.com/group/phillyrb.
For more options, visit https://groups.google.com/d/optout.


Walter Lee Davis

unread,
Apr 21, 2015, 2:28:01 PM4/21/15
to phil...@googlegroups.com
That's what I thought. It kept not working. I'll check what I did later, enjoying the conf right now. 

Walter

Jason Garber

unread,
Apr 21, 2015, 3:33:16 PM4/21/15
to phil...@googlegroups.com, phil...@googlegroups.com
Sure, rub it in whydontcha! :-)

Walter Lee Davis

unread,
Apr 21, 2015, 5:17:52 PM4/21/15
to phil...@googlegroups.com
As I recalled (just tried this all again) the following all errored out:

<%= render partial: 'edit', collection: @slides %>

(missing partial error, it tries to find it in views/lightboxes)

<%= render partial: 'slides/edit', collection: @slides %>

undefined local variable or method `slide' for #<#<Class:0x000000055d8f68>:0x000000055e5f88>
app/views/slides/_edit.html.erb:1:in `_app_views_slides__edit_html_erb__384904886497801069_46013340’

So it seems that `render @collection` has more magic than its long-form `render partial: ‘partial’, collection: @collection` form.

More confused googling ensues...

And for those who may come later (future me, among them) here’s the answer, in the last answer on this SO post:

http://stackoverflow.com/questions/13872003/rails-render-partial-with-collection

Inside my _edit partial, I get passed not a ‘collection_name_singularlized’, as I was expecting, but rather the **name of the partial** as the magic instance variable. Substituting `edit` for `slide` inside the partial as my instance variable name fixed this. No more three lines where one will do.

But I don’t know how I feel about this convention. The render collection behavior is to unroll the collection and pass each member as an instance variable into the partial, so I would really expect these instances to be collection_model_name rather than partial_file_name_without_the_underscore_prefix. It just seems more object-oriented to rely on the object, rather than the local file implementation, for your naming.

Walter


> On Apr 21, 2015, at 2:17 PM, Jason Garber <j...@jasongarber.com> wrote:
>
Reply all
Reply to author
Forward
0 new messages