Displaying pic for user through a question's answer
Posted
by bgadoci
on Stack Overflow
See other posts from Stack Overflow
or by bgadoci
Published on 2010-05-25T04:54:31Z
Indexed on
2010/05/25
5:01 UTC
Read the original article
Hit count: 260
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
© Stack Overflow or respective owner