sent-hil
sent-hil

Reputation: 19285

Rendering view of controller in another controller in Rails

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.

Upvotes: 0

Views: 284

Answers (1)

Cody Caughlan
Cody Caughlan

Reputation: 32748

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

Upvotes: 1

Related Questions