Skip to content

Instantly share code, notes, and snippets.

@nickws
Created July 22, 2011 23:03
Show Gist options
  • Save nickws/1100637 to your computer and use it in GitHub Desktop.
Save nickws/1100637 to your computer and use it in GitHub Desktop.
#ERROR
ActionView::Template::Error (undefined method `voter_emotions' for nil:NilClass):
1: .subject_box{"data-emotions" => subject.voter_emotions.map{|e| e.name}.join(' ')}
2:
3: %p.title
4: %a{:href => "#"} #{subject.title}
app/views/profile/_subject_box.haml:1:in `_app_views_profile__subject_box_haml__657605790416744297_2168763000'
app/controllers/profile_controller.rb:79:in `fetch_subject'
///
#profile_controller.rb
class ProfileController < ApplicationController
before_filter :authenticate_user!
# Home page of the user
def home
@user = current_user
followee_ids = current_user.followee_ids_accepted
@subjects = posted_by_user_id(followee_ids << current_user.id)
@subjects_pagination_data = @subjects.shift
@filter_emotions = Emotion.all
@bond_requests = Bond.find_all_by_followee_id_and_status(current_user.id, 'pending')
respond_to do |format|
format.html {
if request.xhr?
render 'home.ajax', :layout => false
end
}
end
end
# profile page of user, when viewed by other users
def view_profile
#if params[:username]
@user = User.find_by_nickname(params[:username])
@subjects = posted_by_user_id(@user.id)
@filter_emotions = Emotion.all
end
def follow_or_unfollow
user = User.find(params[:user_id])
if current_user.follows?(user)
current_user.unfollow(user)
@action = 'unfollowed'
else
current_user.follow(user)
@action = 'followed'
end
render :json => {:action => @action}
end
def user_votes_via_json
@user = current_user
@content_voted_on = @user.votes.group(:content_id).select([:url, :content_id])
if params[:filter_emotions]
filter_emotions = params[:filter_emotions].split(",")
@content_voted_on = @content_voted_on.find(:all,
:conditions => ['emotion IN (?)',filter_emotions ])
end
render :json => @content_voted_on
end
def fetch_subject
require 'open-uri'
unless params[:url]
return render :json => {:error => 'empty url'}
end
@subject = []
@subject << Subject.find_or_create_by_url(params[:url]) do |subject|
document = Hpricot(open(params[:url]), 'User-Agent' => 'Emot.io/0.1')
page = {}
subject.title = (document/"title").first.try(:inner_html)
subject.description = (document/"meta[subject.name=description]").first.try(:[], :content)
subject.description ||= ActionController::Base.helpers.strip_tags((document/"p").first.try(:inner_html))
subject.title ||= nil
subject.description ||= nil
end
@subject.first.via = current_user
@user = current_user
render :partial => 'subject_box', :locals => {:subject => @subjects}
end
# Displayes subjects the user(s) have voted on
# @param user_ids [Array] of IDs of the users the subjects will be fetched for
# @return [Subject] array of the subjects
def posted_by_user_id(user_ids)
votes = Vote.select('DISTINCT ON (subject_id) subject_id, *').order('subject_id,
created_at DESC').where(:user_id => user_ids).page(params[:page]).per(2)
posts = votes.map {|vote|
subject = vote.subject
subject.via = vote.user
subject.voter_emotions = subject.emotions_felt_by_user(vote.user)
subject.voter_time_last_voted = vote.updated_at
subject
}
posts.unshift(votes)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment