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.
Related
I'm using the acts_as_votable gem to like and unlike "Deals" in my Ruby on Rails project. My user is set to act_as_voter and my deal is set to acts_as_votable, but for some reason everything is set to like as soon as a new user is created, and they can't unlike the deal. For some reason my list of deals all have an unlike button and it doesn't actually do anything but refresh the page. Here's some of my code.
app/views/catalog/index.html.erb
<ul class="deals_list">
<% #deals.each do |deal| %>
<li>
<div>
...
<div class="favorite">
<% if account_signed_in? and current_account.accountable_type == "Personnel" %>
<%= image_tag("dark-favorite.png") %>
<% if deal.liked_by current_account %>
<%= link_to unlike_deal_path(deal), method: :put do %>
Unlike
<% end %>
<% else %>
<%= link_to like_deal_path(deal), method: :put do %>
Like
<% end %>
<% end %>
<% end %>
</div>
</li>
<% end %>
</ul>
app/controllers/deals_controller.rb
def like
#deal = Deal.find(params[:id])
#deal.liked_by current_account
redirect_back(fallback_location: catalog_index_url)
end
def unlike
#deal = Deal.find(params[:id])
#deal.unliked_by current_account
redirect_back(fallback_location: catalog_index_url)
end
config/routes.rb
resources :deals do
member do
put 'like', to: "deals#like"
put 'unlike', to: "deals#unlike"
end
end
Be sure and read the entire Readme because you're using the library wrong.
To check if a voter has voted on a model, you can use voted_for?. You can check how the voter voted by using voted_as_when_voted_for.
I zeroed in on your problem because I was expecting to see a "?" after the deal.liked_by call, which would indicate a boolean result (by convention, not always the case).
So use this instead:
<% if current_account.voted_for? deal %>
The following Rails 4 link_to is wrong, and thus I'm unable to delete and not sure why. In this project, "bookmarks" is a nested resource under "users" so rake routes gives me:
DELETE /users/:user_id/bookmarks/:id(.:format) bookmarks#destroy
View:
<% #bookmarks.each do |bookmark| %>
<%= link_to "delete", user_bookmarks_path(#user, bookmark.id), method: :delete %>
<% end %>
Controller:
def destroy
#user.bookmarks.find(params[:id]).destroy
redirect_to root_path
end
private
def bookmark_params
params.require(:bookmark).permit(:title, :bookmark_url)
end
def get_user
#user = User.friendly.find(params[:user_id])
end
The result is a link that looks like http://www.example.com/users/jane-doe/bookmarks.6 where 6 is the correct ID of the bookmark to be deleted. But I don't understand why it's not creating /bookmarks/6, which I think would then work fine with destroy in my controller. It feels like there's some big conceptual piece I'm just not understanding. Any tips are appreciated.
Your view should look something like this. It appears your path name is incorrect:
<% #bookmarks.each do |bookmark| %>
<%= link_to "delete", user_bookmark_path(#user, bookmark.id), method: :delete %>
<% end %>
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| %>
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 :)
I am trying to Implement a feed of all post from a multi user blog. I want to know where to define this method and how to define it to be as "RESTFUL" as possible.
I am thinking of putting it in the posts index view but the problem is i dont have access to the users name attribute that created that post. The index action currently looks like this
def index
#posts = Post.all
end
and doing this:
def index
#user=User.find(params[:user_id])
#posts = #user.posts.all
end
raises an error " Couldn't find User without an ID "
App info:
I have a users resource and a post resource (nested in the users). That is pretty much it.
Thanks
Clarification:
Thanks guys for the assistance so far. My controllers index action is defined as follows
def index
#users = User.all
#posts = #users.collect { |user| user.posts }.flatten
end
The issue i am having is displaying the posts users name in the view. for example this works but only displays the posts attibutes:
<% #posts.each do |post| %>
<ul>
<li> <%= post.title %>
<%= post.content %>
<%= user.name %> or <%= #user,name %> #This does not work
</li>
</ul>
I am not sure if i wrote the block correctly. Hope this clarifies things
You can do something like this:
def index
#posts = Post.includes(:user)
end
# view
<ul>
<% #posts.each do |post| %>
<li>
<%= post.title %>
<%= post.content %>
<%= post.user.name %>
</li>
<% end %>
</ul>
You should be visiting the wrong URL.
First, check your parameters in your server console, you should not see user_id param.
To set this param, use the correct route
user_posts_path(an_user_instance)
I suppose you have in routes:
resources :users do
resources :posts
end
First you need to query the users that you want...
def index
# For example...
#users = User.all # or...
#users = User.find_by_group_id(params[:group_id]) # etc...
# Then, from your users, you need to collect the posts...
#posts = #users.collect { |user| user.posts }.flatten
end
This would get an array of each users posts, then flatten it to a single array. At that point you can then sort the array to whatever you want.
I don't know what exactly you're looking for, but these are just some ideas. Depending on how you're app is structured (and the groups of users are organized), you'll likely want to use some sorted of nested resource like juanpastas posted.