views:

29

answers:

1

Ok, I am trying to display the profile pic of a user. The application I have set up allows users to create questions and answers (I am calling answers 'sites' in the code) the view in which I am trying to do so is in the /views/questions/show.html.erb file. It might also be of note that I am using the Paperclip gem. Here is the set up:

Associations

Users

class User < ActiveRecord::Base

  has_many :questions, :dependent => :destroy
  has_many :sites, :dependent => :destroy
  has_many :notes, :dependent => :destroy
  has_many :likes, :through => :sites , :dependent => :destroy
  has_many :pics, :dependent => :destroy
  has_many :likes, :dependent => :destroy


end

Questions

class Question < ActiveRecord::Base
  has_many :sites, :dependent => :destroy
  has_many :notes, :dependent => :destroy
  has_many :likes, :dependent => :destroy
  belongs_to :user
 end

Answers (sites)

class Site < ActiveRecord::Base

  belongs_to :question
  belongs_to :user
  has_many :notes, :dependent => :destroy
  has_many :likes, :dependent => :destroy

  has_attached_file :photo, :styles => { :small => "250x250>" }
end

Pics

class Pic < ActiveRecord::Base
  has_attached_file :profile_pic, :styles => { :small => "100x100" }
  belongs_to :user
end

The /views/questions/show.html.erb is rendering the partial /views/sites/_site.html.erb which is calling the Answer (site) with:

<% div_for site do %>
<%=h site.description %>
<% end %>

I have been trying to do things like:

<%=image_tag site.user.pic.profile_pic.url(:small) %>
<%=image_tag site.user.profile_pic.url(:small) %>

etc. But that is obviously wrong. My error directs me to the Questions#show action so I am imagining that I need to define something in there but not sure what. Is is possible to call the pic given the current associations, placement of the call, and if so what Controller additions do I need to make, and what line of code will call the pic?

UPDATE: Here is the QuestionsController#show code:

  def show

    @question = Question.find(params[:id])
    @sites = @question.sites.all(:select => "sites.*, SUM(likes.like) as like_total",
             :joins => "LEFT JOIN likes AS likes ON likes.site_id = sites.id",
             :group => "sites.id",
             :order => "like_total DESC")

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @question }
    end
  end
A: 

You have has_many association for the pics:

class User < ActiveRecord::Base
  ...
  has_many :pics, :dependent => :destroy
end

But you are trying to act, as if there was only one pic:

<%=image_tag site.user.pic.profile_pic.url(:small) %>

So either take first picture (probably you should also check if it exists) :

<%=image_tag site.user.pics.first.profile_pic.url(:small) %>

Or change the association to has_one if user can have only one picture:

class User < ActiveRecord::Base
  ...
  has_one :pic, :dependent => :destroy
end

<%=image_tag site.user.pic.profile_pic.url(:small) %>
Voyta
That worked perfectly. Thank you.
bgadoci