Forum: Ruby on Rails render partial

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
97bd2f0974055dacf53a0643b013b32e?d=identicon&s=25 Petan Cert (petan)
on 2008-12-12 13:50
Hi,

I am trying to use paperclip plugin to add images to the posts.

<%= render :partial => 'recent_posts', :collection => @posts %>
@posts = Post.find(:all, ..., :limit => 5)

#_recent_posts.rhtml
<%= image_tag recent_posts.photo.url(:thumb) %>

and it will end up with this error:
"undefined method `symbolize_keys!' for \"/posts/24?user_id=1\":String"

Anybody knows what does this error mean?
Thank you.
97bd2f0974055dacf53a0643b013b32e?d=identicon&s=25 Petan Cert (petan)
on 2008-12-12 16:27
I guess it is because I am not passing the post and user id. If I use
<%= image_tag @post.photo.url(:thumb) %> in my show action in posts
controller it is working fine. But, I still dont know how to display
photos in my collection.
Any suggestions?

Thank you.
904e1a154601737b8e275a220432abb7?d=identicon&s=25 Bob Proulx (Guest)
on 2008-12-19 18:43
(Received via mailing list)
Petan Cert wrote:
> <%= render :partial => 'recent_posts', :collection => @posts %>
> @posts = Post.find(:all, ..., :limit => 5)

Using the new Rails method is prettier.  If Rails sees @recent_posts
it will automatically assume the name for the partial and pass through
recent_posts as a local posts variable.

  <%= render :partial => @recent_posts %>

> #_recent_posts.rhtml
> <%= image_tag recent_posts.photo.url(:thumb) %>
>
> and it will end up with this error:
> "undefined method `symbolize_keys!' for \"/posts/24?user_id=1\":String"

The :collection passed through is @posts.  But you are referencing
recent_posts instead which is different.

If you go with rails iterating through the collection then the
singularization of the plural name will create "recent_post" from
"@recent_posts" and you can reference the name that way.

  <%= image_tag recent_post.photo.url(:thumb) %>

If passing in @recent_posts doesn't make sense then you would need to
reference through @posts with something like

  post.photo.url(:thumb)

where post was created by rails for iterating over the @posts
collection.

  http://api.rubyonrails.org/classes/ActionControlle...

Bob
This topic is locked and can not be replied to.