Listing users that voted on post using acts_as_votable - ruby-on-rails

I'm trying to display the images of the users who've upvoted a post using the acts_as_votable gem, exactly like this question. The problem is the answer that worked for that question is not working in my case.
I display the upvotes count like this:
<%= #post.cached_votes_total %>
I've added 'acts_as_votable' and 'acts_as_voter' into the post and user models.
The controller:
def upvote
#post = Post.find params[:id]
#post.liked_by current_user
end
The accepted answer in the above question is this:
<% #post.votes_for.voters.each do |p| %>
<%= image_tag(p.image) %>
<% end %>
However this gives me an 'undefined method `votes_for' ' error.
<% #post.votes.each do |user| %>
<%= user %>
<% end %>
This gives no error but I can't access the user's image.

Ok, I got it by playing around a bit more.
<% #post.votes.by_type(User).voters.each do |user| %>

Related

Resolve SoundCloud track in controller

First off, I'm pretty new to Ruby on Rails and the SoundCloud API so sorry if this is a stupid question.
My problem is, I'm trying to use the SoundCloud resolve track feature to get track information about a post.
Right now I have a scaffold set up that runs through each post using the
<% #posts.each do |post| %>
<%= post.soundcloud %>
<% end %>
.soundcloud is just the url of the song that I want to be resolved.
I've tried
<%= #client.get('/resolve', :url => <%= post.soundcloud %> ) %>
But then realized I can't have nested erb tags.
I've also tried passing various methods in the controller but I can't seem to figure it out.
Oh, here is what my controller looks like.
def index
require 'soundcloud'
#posts = Post.all
#client = Soundcloud.new(:client_id => 'MY_CLIENT_ID')
#tracks = #client.get('/resolve', :url => '') <-- Don't know what to do with this
end
Any suggestions?
Thanks in advance
I didnt check the documentation, but from looking at your code your erb file should be
<% #posts.each do |post| %>
<%= #client.get('/resolve', url: post.soundcloud ) %>
<% end %>

In loop, find if two records belong to same post

I have a loop of questions where each question belongs to a post and the post title is displayed above the question. If 2 ( or 3 or 4 etc) questions in a row belong to the same post I only want to display the post name once. My idea was to use an index to check if the prior questions post == the current questions post. The problem is I'm not sure how that would work.
Here is what I tried:
<% #questions.each_with_index do |question, i| %>
<% unless (i-1).comment.post == question.comment.post %>
<%= question.comment.post.title %>
<% end %>
<% end %>
This gives me an undefined method comment error since I can't call '(i-1).comment' but can I do something like that or is there a better way to do this?
One way to do this would be to use group_by to group the questions by post:
<% #questions.group_by {|q| q.comment.post}.each do |post, questions| %>
<%= post.title %>
<% for question in questions %>
<%= question.title %>
<% end %>
<% end %>
If you need to maintain the order of the questions, you could use each_cons.
<%= #questions.first.comment.post.title %>
<% #questions.each_cons(2) do |previous_question, question| %>
<% unless previous_question.comment.post == question.comment.post %>
<%= question.comment.post.title %>
<% end %>
<% end %>
Couple of minor notes:
This is a troubling Law of Demeter violation (having to traverse from the question to the comment to the post and eventually the title).
This sort of complicated data manipulation is much better in a Ruby object than in a template. Ideally, templates are dead simple.

Devise - how to show user's post

I use Devise gem for authentication.
In database I have users table and posts table in my database schema (and Post controller).
In post controller I want to find all posts assigned to specific user. I have user_id in posts table.
How to get all user's posts or how to check if specific post is assigned for SIGNED IN user.
I thought about something like this (of course is only pseudocode:
current_user.id == Post.where(params:[post_id]).user_id
So how to get current user id in Devise and how to check the current user id is the same like eg. user_id assigned to viewing post (I want to add 'edit' function when current user is post owner) and how to find all post which current user is owner.
Associations
Firstly, your user_id column in your posts table is what's known as a foreign_key
Foreign keys are used in relational database systems to give you the ability to call associative data from a single record. Simply, it means that you'll be able to use the ActiveRecord associations to call the data you require, rather than having to call it individually:
#app/models/user.rb
class User < ActiveRecord::Base
has_many :posts
end
#app/models/post.rb
class Post < ActiveRecord::Base
belongs_to :user
end
This will give you the ability to use the following call:
#app/controllers/posts_controller.rb
class PostsController < ApplicationController
def index
#posts = current_user.posts
end
end
You'll be best served looking up the has_many association:
Fix
In regards to showing your posts for your users, you need to be sure that you have the correct "flow" set up. What I mean is you need some condition to know whether your user is signed in & that #posts is set:
#app/views/posts/index.html.erb
<% if #posts.present? %>
<% #posts.each do |post| %>
<%= post.title %>
<% end %>
<% end %>
Maybe this is the first time you use Devise. You can access current_user inside controllers or views. I imagine you could do something like this
In controller (posts_controller.rb):
#posts = current_user.posts
In view (posts/show.html.erb, I guess):
if current_user.id = #post.current_user
#render something here
end
Get all post which current user is owner.
#posts = Post.where(:user_id => current_user.id)
and on your view
<%-# commented : checking if #posts is empty -%>
<% if #posts.empty? %>
<span>Sorry, post is empty </span>
<% else %>
<%= #posts.each do |p| %>
<% if p.user_id == current_user.id %>
<% link_to "edit", edit_path(p) %>
<% end %>
<% end %>
<% end %>
There are many ways you could get current_user posts. I'll go the long way.
we need
an action
an action view and a partial
a route
a link_to
* action *
def my_posts
#posts = current_user.posts.all.order(created_at: 'DESC')
end
* view *
my_posts.html.erb
<% if #posts.present? %>
<%= render 'posts' posts: #posts %>
<% else %>
<h1>You don't have any posts yet! create one</h1>
<% end %>
_posts.html.erb
<%posts.each do |post| %>
<%= post.title %>
<% end %>
index.html.erb
<%= render 'posts' posts: #posts %>
route
get 'post' => 'posts#my_posts', as: :my_posts
link_to
<%= link_to 'My posts', my_posts_path %>
I may be late but someone can find it useful :)

Rails getting activity feed that only involves current_user

I'm using the public_activity gem on my rails app to get all of the current_users followers activity. It works great but I'd prefer to get only the activity that relates to the current_user so it's more of a Notification system than just Activities.
Here is what I have so far in my activities controller
#activities_all = PublicActivity::Activity.order("created_at desc").where(owner_type: "User", owner_id: current_user.followed_users.map {|u| u.id}).all
#activities = #activities_all.group_by { |t| t.created_at.beginning_of_day }
As stated before, this gives all the activities that the current_user's followers do (like create/comment on a post). I'd prefer it if I could get it to work where it's all the activities where my followers interact with my posts, etc. So when a user comments and that comment is on my post, then i want that to be part of #activities.
At first glance, I would figure this is another query I just cannot figure out the query (if query is the correct way).
Here is some front end code if that will help
<% #activities.sort.each do |day, activities| %>
<h6><%= day.strftime("%B %d") %></h6>
<% for activity in activities %>
<% if activity.trackable %>
<div class="activity">
<p>
<%= activity_user_image(activity.owner) %>
<%= activity_user_name(activity.owner) if activity.owner %>
<%= render_activity activity %>
<span><%= activity.created_at.strftime("%I:%M %p") %></span>
</p>
</div>
<% end %>
<% end %>
<% end %>
Thanks for the help all. Happy Holidays
In case anyone else have this question down the road, I found the answer
First, I added a recipient to each activity.
if #comment.save
#comment.create_activity :create, owner: current_user, recipient: #design.user
end
Then i added another query to the activities
#activities_all = PublicActivity::Activity.order("created_at desc").where(recipient_id: current_user).where("owner_id not in (?)", current_user).all
Not really sure about your relationships, but something like this should work:
#user_activities = PublicActivity::Activity.order("created_at desc").where(owner_type: "User", owner_id: current_user.id).all

Rails Link_to with Loop - RESTful routes

A question that I hope you can answer for a Q&A app. Still very new with Rails. It should be fairly simple but there is a small issue that I run into whenever I am trying to create a list of links to show up that point to a RESTful route from a loop.
Here's the code for the controller:
users_controller.rb
def list
#user = User.find(params[:id])
#users = User.find(:all, :select => :name)
end
def quiz
#user = User.find(params[:id])
#users = User.find(:all, :select => :name)
end
Here's the code for the view:
<% if current_user.admin? %>
<ul>
<%- #users.each do |link| %>
<li><%= link_to link.name, quiz_user_path(#user, #quiz) %></li>
<% end %>
</ul>
<% end %>
Here's the code for the route:
resources :users do
get 'quiz', :on => :member
end
What I want to do is generate individual links based on the name of the users and then link to the quiz page for that specific user. I'm pretty sure that something needs to be changed for the code in my view. Right now, all I'm getting is a link that all points to the current user which in this case is user 4. (http://localhost:3000/users/4/quiz)
Thanks for any quick tips to solve this.
Currently you are using the same #user instance variable for each link. Instead you need to use the variable set in your loop. The code below should work as expected:
<% if current_user.admin? %>
<ul>
<%- #users.each do |user| %>
<li><%= link_to user.name, quiz_user_path(user) %></li>
<% end %>
</ul>
<% end %>
I also renamed the variable from link to user, for clarity, since your iterating through users not links.
It's because in your current route map quiz_user_path needs only one variable. Try to remove #quiz.

Resources