0

The same question has been asked many times, but I am unable to find a solution that works for me.

I have three models, posts, users, and picture (paperclip).

I use restful_authentication and set it up so that each user sees only his/her post in posts/index. I've also given him the ability to upload a profile picture.

My question is how do I take the picture/index and show it in post/index. I want to show a thumbnail on top of the post/index page, followed by the posts.

I've used partials, but I get the error

NoMethodError in Posts#index

Showing app/views/us/_picture.html.erb where line #1 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.each

Extracted source (around line #1):

    1: <% for u in @us %>
    2:   <tr>
    3:     <td><%= image_tag u.picture.url(:thumb) %></td>
    4:     <td><%=h u.name %></td>

I see what it's trying to do, how do I solve it so that it just shows the uploaded picture.

Thanks for your help.

1 Answer 1

1

The error is saying that you havent assigned the @us instance variable to the template, which you normally do in the controller.

The template compilation step is having issues at the first iteration step (iterating over the @us array), it hasnt even gotten to the point of trying to run the picture accessing/rendering step, which might or might not have problems of its own.

In your controller grab a set of users (with pictures) and assign it to @us, some basic pseudo-code:

class PostsController < ApplicationController
  def index
   @us = User.find(:limit => 10, :include => [:picture])
  end
end
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for you reply. I'm getting "Couldn't find U without an ID". Could it be because I don't have picture belonging to the user (not under restful_authentication?)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.