Rails : Couldn't find Item with 'id'= - ruby-on-rails

I'm trying to implement add to favorites and I'm getting this error:
ActiveRecord::RecordNotFound in FavoriteItemsController#index
Couldn't find Item with 'id'=
this is where the error pops up, at the following link:
<%= link_to "Favorite Items", favorites_path, method: :get, :class => 'navbar-link' %>
model associations
class Favorite < ApplicationRecord
belongs_to :viewer
belongs_to :favorited, polymorphic: true
end
class Viewer < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :favorites
has_many :favorite_items, through: :favorites, source: :favorited, source_type: 'Item'
end
routes:
get '/favorites', to: 'favorite_items#index', as: 'favorites'
resources :favorite_items, only: [:create, :destroy]
and the add or remove favorites links on my show page:
<%- unless current_shopper.favorite_items.exists?(id: #item.id) -%>
<%= link_to 'Add to favorites', favorite_items_path(item_id: #item), method: :post %>
<%- else -%>
<%= link_to 'Remove from favorites', favorite_item_path(#item), method: :delete %>
<%- end -%>
the controller
class FavoriteItemsController < ApplicationController
before_action :set_item
def index
#favorites = current_viewer.favorites
end
def create
if Favorite.create(favorited: #item, viewer: current_viewer)
redirect_to #item, notice: 'Item has been favorited'
else
redirect_to #item, alert: 'Something went wrong...*sad panda*'
end
end
def destroy
Favorite.where(favorited_id: #item.id, viewer_id: current_viewer.id).first.destroy
redirect_to #item, notice: 'Item is no longer in favorites'
end
private
def set_item
#item = Item.find(params[:item_id] || params[:id])
end
and the index to loop thru the favored items, although not sure if this is correct since the error stops before the loop:
<% #favorites.each do |item| %>
<tr>
<td><%= item.title %></td>
<td><%= item.price %></td>
<td><%= link_to 'Remove from favorites', favorite_items_path(#item.id), method: :delete %></td>
< /tr>
<% end %>
Update 1
This is what I get when I click on add to favorites
Item Exists (4.3ms) SELECT 1 AS one FROM "items" INNER JOIN "favorites"
ON "items"."id" = "favorites"."favorited_id" WHERE "favorites"."viewer_id"
= $1 AND "favorites"."favorited_type" = $2 AND "items"."id" = $3 LIMIT $4
[["viewer_id", 2], ["favorited_type", "Item"], ["id", 4], ["LIMIT", 1]]
items_controller.rb
def create
#item = Item.new(item_params)
respond_to do |format|
if #item.save
format.html { redirect_to #item, notice: 'Item was successfully created.' }
format.json { render :show, status: :created, location: #item }
else
format.html { render :new }
format.json { render json: #item.errors, status: :unprocessable_entity }
end
end
end
def show
#comments = Comment.where(item_id: #item).order("created_at DESC")
#items = Item.find(params[:id])
end

class FavoriteItemsController < ApplicationController
before_action :set_item, only: [:create, :destroy]
..
..
end
Index html
<%= link_to 'Remove from favorites',
favorite_items_path(#item.id), method: :delete %>
replace with
<%= link_to 'Remove from favorites', favorite_item_path(item.id),
method: :delete %>

Try replacing you FavoriteItemsController with
before_action :set_item, :only => [:create, :destroy]

Related

Simple_form_for, comment don't appear as expected

I have problem with the comments.
If I comment a status whatever it is, the comment will be displayed on the last status.
Hope you could help, I can edit with more code if needed, let me know what you need.
_form.html.erb
<%= simple_form_for([status, status.comments.new]) do |f|%>
<%= f.input :content %>
<%= f.button :submit, "Comment", class:"btn btn-primary"%>
<% end %>
index.html.erb
<% #statuses.each do |status| %>
<%= image_tag status.user.avatar.thumb if status.user.avatar?%>
<%= status.user.full_name%>
<%= simple_format(status.content) %>
<%= link_to time_ago_in_words(status.created_at) + " ago", status %>
<% if status.user == current_user %>
<span class="admin">
<%= link_to "Edit", edit_status_path(status) %> |
<%= link_to "Delete", status, method: :delete, data: {confirm: "Are you sure?"} %>
</span>
<% end %>
<% status.comments.each do |comment| %>
<%= comment.content %>
<% end %>
<%= render partial: "comments/form", locals: {status: status} %>
<% end %>
routes.rb
Rails.application.routes.draw do
get 'profiles/show'
devise_for :users
devise_scope :user do
get 'register', to: 'devise/registrations#new', as: :register
get 'login', to: 'devise/sessions#new', as: :login
get 'logout', to: 'devise/sessions#destroy', as: :logout
end
resources :statuses do
resources :comments
end
resources :comments
get 'feed', to: "statuses#index", as: :feed
root "statuses#index"
get '/:id', to: "profiles#show"
end
statuses_controller.rb
class StatusesController < ApplicationController
before_filter :authenticate_user!, only: [:new, :create, :edit, :update]
before_action :set_status, only: [:show, :edit, :update, :destroy]
def index
#users = User.all
#statuses = Status.all
#comments = Comment.all
end
def show
#status = Status.find(params[:id])
#comments = #status.comments.all
end
def new
#status = Status.new
#comment = #status.comments.build
end
def create
#status = Status.new(status_params)
#status.user = current_user
respond_to do |format|
if #status.save
format.html { redirect_to #status, notice: 'Status was successfully created.' }
format.json { render :show, status: :created, location: #status }
else
format.html { render :new }
format.json { render json: #status.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #status.update(status_params)
format.html { redirect_to #status, notice: 'Status was successfully updated.' }
format.json { render :show, status: :ok, location: #status }
else
format.html { render :edit }
format.json { render json: #status.errors, status: :unprocessable_entity }
end
end
end
def destroy
#status.destroy
respond_to do |format|
format.html { redirect_to statuses_url, notice: 'Status was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_status
#status = Status.find(params[:id])
end
def status_params
params.require(:status).permit( :content)
end
end
comments_controller.rb
class CommentsController < ApplicationController
def create
#status = Status.find_by(params[:status_id])
#comment = #status.comments.create(params_comment)
#comment.save
redirect_to statuses_path
end
def index
#statuses = Status.all
#comments = Comment.all
#comment = Comment.find_by(params[:id])
end
def new
#status = Status.find(params[:status_id])
#comment = Comment.new
end
private
def params_comment
params.require(:comment).permit(:content)
end
end
my models
class Comment < ActiveRecord::Base
belongs_to :status
belongs_to :user
end
class Status < ActiveRecord::Base
belongs_to :user
has_many :comments
default_scope -> { order(created_at: :DESC)}
validates :content, presence: true,
length: {minimum: 2}
validates :user_id, presence: true
end
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
mount_uploader :avatar, AvatarUploader
validates_presence_of :avatar
validates_integrity_of :avatar
validates_processing_of :avatar
has_many :statuses
has_many :users
validates :first_name, presence: true
validates :last_name, presence: true
validates :profile_name, presence: true,
uniqueness: true,
format: {
with: /\A[a-zA-Z0-9_-]+\z/,
message: "Must be formatted correctly." }
def full_name
first_name + " " + last_name
end
def gravatar_url
stripped_email = email.strip
end
end
Thank you
This is when I commen as "Nelly Johan" here on my own status...
And this my comment not where I expected:

Deleting Nested Reviews Deletes the whole Post Created

I am setting a nested review scaffold inside the Post scaffold however, when i try to delete a review that is nested inside the show page in the Post, The whole post is deleted. how can i delete only the reviews without the post?
here's my code:
posts_controller.rb
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user! , except: [:index,:show]
before_filter :check_user, only: [:edit,:update,:destroy]
# GET /posts
# GET /posts.json
def search
if params[:search].present?
#posts = Post.search(params[:search])
else
#posts = Post.all
end
end
def index
if params[:tag]
#posts = Post.tagged_with(params[:tag])
else
#posts = Post.all
end
end
# GET /posts/1
# GET /posts/1.json
def show
#reviews = Review.where(post_id: #post.id)
end
# GET /posts/new
def new
#post = Post.new
end
# GET /posts/1/edit
def edit
#post = Post.find(params[:id])
end
# POST /posts
# POST /posts.json
def create
#post = Post.new(post_params)
#post.user_id = current_user.id
respond_to do |format|
if #post.save
format.html { redirect_to #post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: #post }
else
format.html { render :new }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if #post.update(post_params)
format.html { redirect_to root_url, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: #post }
else
format.html { render :edit }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
#post.destroy
respond_to do |format|
format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
#post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:title, :description,:image,:all_tags)
end
def check_user
if current_user.id != #post.user_id
redirect_to root_path , alert: "Sorry this Post belongs to someone else"
end
end
end
routes.rb
Rails.application.routes.draw do
devise_for :users
resources :posts do
collection do
get 'search'
end
resources :reviews , except: [:show,:index] do
member do
get "like" => "reviews#upvote"
get "dislike" => "reviews#downvote"
end
end
end
get 'pages/help'
get 'pages/blog'
get 'pages/contact'
get 'pages/tour'
resources :posts
root 'posts#index'
get 'tags/:tag', to: 'posts#index', as: "tag"
end
reviews_controller.rb
class ReviewsController < ApplicationController
before_action :set_review, only: [ :edit, :update, :destroy, :upvote,:downvote]
before_action :set_post
before_action :authenticate_user!
respond_to :html
def new
#review = Review.new
respond_with(#review)
end
def edit
end
def create
#review = Review.new(review_params)
#review.user_id = current_user.id
#review.post_id = #post.id
#review.save
redirect_to post_path(#post)
end
def update
#review.update(review_params)
respond_with(#post)
end
def destroy
#review.destroy
respond_with(#review)
end
def upvote
#review.upvote_from current_user
redirect_to :back
end
def downvote
#review.downvote_from current_user
redirect_to :back
end
private
def set_review
#review = Review.find(params[:id])
end
def set_post
unless #post = Post.where(id: params[:post_id]).first
redirect_to posts_path, flash: {alert: "Post doesn't exists"}
end
end
def review_params
params.require(:review).permit(:comment)
end
end
models/review.rb
class Review < ActiveRecord::Base
acts_as_votable
belongs_to :user
belongs_to :post
end
models/post.rb
class Post < ActiveRecord::Base
searchkick
has_many :reviews , dependent: :destroy
has_many :taggings
has_many :tags, through: :taggings
#Paperclip Installation
has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
def all_tags=(names)
self.tags = names.split(",").map do |name|
Tag.where(name: name.strip).first_or_create!
end
end
def all_tags
self.tags.map(&:name).join(", ")
end
def self.tagged_with(name)
Tag.find_by_name!(name).posts
end
end
views/posts/index.html.erb
<table class="table">
<thead>
<tr>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #posts.each do |post| %>
<tr>
<td><h4><%=link_to post.title , post%></h4></td>
<td><%=raw tag_links(post.all_tags)%></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<%end%>
</tbody>
</table>
<%= link_to 'new post', new_post_path %>
views/posts/show.html.erb
<div class="center">
<div class="right-align">
<h2><%= #post.title %></h2>
<hr>
</div>
<%if #post.image.exists?%>
<%= image_tag #post.image.url(:medium) %>
<%end%>
<div class="right-align">
<%= markdown #post.description %>
<br>
<table class="table table-bordered">
<tbody>
<% #reviews.each do |review|%>
<tr>
<td >
Welcome back <%= current_user.name %>
<h4><%= link_to "like" ,like_post_review_path(#post, review) , class: " btn btn-primary glyphicon glyphicon-chevron-up"%></h4>
<p><%= review.get_upvotes.size%></p>
<p><%= review.get_downvotes.size%></p>
<h4><%= link_to "Dislike" , dislike_post_review_path(#post, review) , class: "btn btn-primary glyphicon glyphicon-chevron-down"%></h4>
<p><%= markdown review.comment %></p>
<p><%= link_to "Edit", edit_post_review_path(#post, review) %></p>
<p><%= link_to 'Destroy', #review, method: :delete, data: { confirm: 'Are you sure?' } %></p>
</td>
</tr>
<%end%>
</tbody>
</table>
<p><%= link_to 'Write Review', new_post_review_path(#post) , class: "btn btn-primary" %></p>
<%= link_to 'Edit', edit_post_path(#post) %> |
<%= link_to 'Back', posts_path %>
</div>
</div>
Update:
i tried changing the code as suggested in the destroy method and in the Reviews Controller and the Post Show page but still getting the error. Here's the error i am getting :
Look like a typo, change:
<p><%= link_to 'Destroy', #review, method: :delete, data: { confirm: 'Are you sure?' } %></p>
to:
<p><%= link_to 'Destroy', review, method: :delete, data: { confirm: 'Are you sure?' } %></p>
and change your reviews controller destroy:
def destroy
#review.destroy
redirect_to :back
end

How to link to a nested route action inside a parent loop?

I'm trying to display everything on my index page, but when I try to link to a new_story_substory_path I get undefined local variable or method substory.
Here is the code:
index.html.erb
<% #stories.each do |story| %>
<h3><p><%= story.title %></p></h3>
<p><%= story.plot %></p>
<% if story.user == current_user %>
<%= link_to 'Show XXX', story_path(story), class: "btn btn-success" %>
<%= link_to 'Edit', edit_story_path(story), class: "btn btn-success" %>
<%= link_to "Delete", story_path(story), method: :delete, data: { confirm: "Are you sure?" }, class: "btn btn-success" %></br>
<% end %>
<% story.substories.each do |substory| %>
<h4><p><%= substory.title %></p></h3>
<p><%= substory.subplot %></p>
<% if story.user == current_user %>
<%= link_to 'Show', story_substory_path(substory.story, substory), class: "btn btn-default" %>
<%= link_to 'Edit', edit_story_substory_path(substory.story, substory), class: "btn btn-default" %>
<%= link_to "Delete", story_substory_path(substory.story, substory), method: :delete, data: { confirm: "Are you sure?" }, class: "btn btn-default" %>
<% end %>
<% end %>
<br>
<% if story.user == current_user %>
<br>
<br>
# this is where I'm getting the error:
<%= link_to 'New subplot', new_story_substory_path(substory.story, substory), class: "btn btn-warning" %>
# however if I move this up, inside the second loop, it works perfectly.
<br>
<% end %>
<% end %>
<br>
<%= link_to 'New Story', new_story_path, class: "btn btn-danger" %>
Here is my controller, routes and models:
class StoriesController < ApplicationController
before_action :set_story, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def index
#stories = Story.all
end
def show
end
def new
#story = current_user.stories.build
end
def edit
end
def create
#story = current_user.stories.build(story_params)
respond_to do |format|
if #story.save
format.html { redirect_to root_path, notice: 'Story was successfully created.' }
format.json { render :show, status: :created, location: root_path }
else
format.html { render :new }
format.json { render json: root_path.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #story.update(story_params)
format.html { redirect_to root_path, notice: 'Story was successfully updated.' }
format.json { render :show, status: :ok, location: root_path }
else
format.html { render :edit }
format.json { render json: #story.errors, status: :unprocessable_entity }
end
end
end
def destroy
#story.destroy
respond_to do |format|
format.html { redirect_to stories_url, notice: 'Story was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_story
#story = Story.find(params[:id])
end
def story_params
params.require(:story).permit(:title, :plot)
end
end
Rails.application.routes.draw do
devise_for :users
resources :stories do
resources :substories
end
root 'stories#index'
end
class Story < ActiveRecord::Base
has_many :substories, dependent: :destroy
belongs_to :user
end
class Substory < ActiveRecord::Base
belongs_to :story
belongs_to :user
end
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :stories, dependent: :destroy
has_many :substories, dependent: :destroy
end
what am I missing?
Edit:
Here is the controller for the substories:
class SubstoriesController < ApplicationController
before_action :set_substory, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
before_action :set_story
def index
#Substories = #story.substories.all
end
def show
end
def new
#substory = Substory.new
end
def edit
end
def create
#substory = Substory.new(substory_params)
#substory.user_id = current_user.id
#substory.story_id = #story.id
if
#substory.save
redirect_to #story
else
render 'new'
end
end
def update
respond_to do |format|
if #substory.update(substory_params)
format.html { redirect_to root_path, notice: 'Story was successfully updated.' }
format.json { render :show, status: :ok, location: root_path }
else
format.html { render :edit }
format.json { render json: #story.errors, status: :unprocessable_entity }
end
end
end
def destroy
#substory.destroy
redirect_to root_path
end
private
def set_story
#story = Story.find(params[:story_id])
end
def set_substory
#substory = Substory.find(params[:id])
end
def substory_params
params.require(:substory).permit(:title, :subplot)
end
end
I am trying to put this as a comment but I need 50 reputation points to do so. Anyway here is my solution:
The problem is index.html comes from Stories Controller. At this point, you are actually not using any other controllers. This is a common problem at a single page application using ROR. Things do get messy this way.
And you are right, you have to move your error on the second loop. The problem is it would show more than once. That is because there is more than one story. If you want to avoid this, the solution I would say is just make an stories#show show.html.erb. Once you click story, it directs to substroy which then has a plot.
I got the answer from the comments.
MrYoshi wrote:
At the line producing the error: You are calling the substory variable but it is only existing in the story.substories.each loop ; you probably meant to use new_story_substory_path(story) (path leading to the creation page of a substory belonging to the story currently been seen/edited)
Basically I changed the line form:
<%= link_to 'New subplot', new_story_substory_path(substory.story, substory), class: "btn btn-warning" %>
to:
<%= link_to 'New subplot', new_story_substory_path(story), class: "btn btn-warning" %>
This works!

Rails 4 ForbiddenAttributesError - Nested Resource

I'm having "ForbiddenAttributesError" in my Rails 4 application. What am I missing here?
Also the problem is, why "examination_id" parameter isn't sent to the request?
Request
Started POST "/examinations/1/participations" for 127.0.0.1 at 2014-03-26 10:47:01 +0200
Processing by ParticipationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"EuGZIXKJE9a1It6Ema5t+g07vXngQoqPMV5qQBfekfg=", "participation"=>{"user_id"=>"1", "examination_id"=>"", "language_preference"=>"İngilizce", "exam_center_preference"=>"1", "disability"=>"0"}, "commit"=>"Sınava Başvur", "examination_id"=>"1"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
Examination Load (0.2ms) SELECT "examinations".* FROM "examinations" WHERE "examinations"."id" = ? LIMIT 1 [["id", "1"]]
Completed 500 Internal Server Error in 5ms
ActiveModel::ForbiddenAttributesError (ActiveModel::ForbiddenAttributesError):
app/controllers/participations_controller.rb:37:in `create'
Routes.rb
resources :examinations do
resources :participations
end
Participation.rb
class Participation < ActiveRecord::Base
belongs_to :user
belongs_to :examination
end
Examination.rb
class Examination < ActiveRecord::Base
has_many :participations
has_many :users, :through => :participations
has_many :exam_fees, dependent: :destroy
has_many :exam_languages, dependent: :destroy
end
participations_controller.rb
#encoding: utf-8
class ParticipationsController < ApplicationController
before_filter :authenticate_user!
before_action :set_participation, only: [:show, :edit, :update, :destroy]
before_filter :get_examination
def get_examination
#examination = Examination.find(params[:examination_id])
end
# GET /participations
# GET /participations.json
def index
#participations = #examination.participations
end
# GET /participations/1
# GET /participations/1.json
def show
#participation = #examination.participations.find(params[:id])
end
# GET /participations/new
def new
#participation = Participation.new
end
# GET /participations/1/edit
def edit
end
# POST /participations
# POST /participations.json
def create
#participation = #examination.participations.new(params[:participation])
#participation.user = current_user
respond_to do |format|
if #participation.save
redirect_to #examination
format.html { redirect_to [#examination, #participation], notice: 'Sınav Katılımınız Oluşturuldu!' }
format.json { render action: 'show', status: :created, location: [#examination, #participation] }
else
render 'new'
format.html { render action: 'new' }
format.json { render json: #participation.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /participations/1
# PATCH/PUT /participations/1.json
def update
respond_to do |format|
if #participation.update(participation_params)
format.html { redirect_to [#examination, #participation], notice: 'Sınav Katılımını Güncellendi!' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #participation.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_participation
#participation = Participation.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def participation_params
params.require(:participation).permit(:user_id, :examination_id, :payment_status, :language_preference, :exam_center_preference, :disability)
end
end
/views/participations/_form.html.erb
<%= simple_form_for([#examination, #participation]) do |f| %>
<%= f.error_notification %>
<fieldset>
<legend>Sınav Katılımı</legend>
<%= f.input :user_id, :as => :hidden, :input_html => { :value => current_user.id } %>
<%= f.input :examination_id, as: :hidden %>
<%= f.input :language_preference, collection: ["Türkçe", "İngilizce", "Rusça"], label: 'Sınav Dili Tercihi' %>
<%= f.input :exam_center_preference, collection:ExamCenter.all, label_method: :city, as: :select, label: 'Sınav Merkezi Tercihi' %>
<%= f.input :disability, inline_label: 'Yardımcı İstiyorum', label: false %>
<%= f.button :submit, "Sınava Başvur" %>
</fieldset>
<% end %>
In order to assign parameters in Rails 4 to object, you should use strong parameters 'syntax' implemented in your participation_params method, instead of passing params directly. So change line:
#participation = #examination.participations.new(params[:participation])
to:
#participation = #examination.participations.new(participation_params)
Since you create your Participation record through association, you don't really need examination_id param in this controller. What's more, if you allow this parameter, it becomes easy to assign Participation to Examination other than from which context you create Participation, which I doubt to be desirable. So I guess you should remove examination_id both from fields in your form and from participation_params method.

undefined method `vote_for_song_songs_path' [rails 4]

So, I'm having trouble getting the Thumbs Up gem workin'.
I've followed this tutorial but I seem to be getting the below errror:
Error msg:
NoMethodError in Songs#index
Showing /Users/apane/Downloads/leap_stage/leap_stage/app/views/songs/index.html.erb where line #10 raised:
undefined method `vote_for_song_songs_path' for #<#<Class:0x007fd0b60203d0>:0x007fd0b26ecdc0>
<li><%= link_to song.title, song %><br></li>
Submitted <%= time_ago_in_words(song.created_at) + " ago" %>
<span class="comments"> | <%= pluralize(song.comments.size, 'comment') %></span><br />
<%=link_to image_tag('thumbs_up', :border => 0), vote_for_song_songs_path(#song), :remote => true %>
<%=link_to image_tag('thumbs_down', :border => 0), vote_against_song_songs_path(#song), :remote => true %>
index.html.erb
<div id="layout1">
<h3>Songs</h3>
<ol>
<% #songs.each do |song| %>
<li><%= link_to song.title, song %><br></li>
Submitted <%= time_ago_in_words(song.created_at) + " ago" %>
<span class="comments"> | <%= pluralize(song.comments.size, 'comment') %></span><br />
<%=link_to image_tag('thumbs_up', :border => 0), vote_for_song_songs_path(#song), :remote => true %>
<%=link_to image_tag('thumbs_down', :border => 0), vote_against_song_songs_path(#song), :remote => true %>
<%#= link_to 'Show', song, class: "button small secondary" %>
<%= link_to('Edit', edit_song_path(song), class: "button small secondary") if can? :update, #song %>
<%= link_to('Destroy', song, method: :delete, data: {confirm: 'Are you sure?'}, class: "button small secondary") if can? :destroy, #song %>
<% end %>
</ol>
</div>
<br />
</div>
song_controller.rb
class SongsController < ApplicationController
before_filter :authenticate_user!, only: [:create ,:edit, :update, :destroy, :vote_for_song]
before_action :set_song, only: [:show, :edit, :update, :destroy, :vote_for_song]
def vote_for_song
#song = Song.find(params[:id])
current_user.vote_for(#song)
respond_to do |format|
format.js
end
end
def vote_against_song
#song = Song.find(params[:id])
current_user.vote_against(#song)
respond_to do |format|
format.js
end
end
# GET /Songs
# GET /Songs.json
def index
#songs = Song.all
end
# GET /Songs/1
# GET /Songs/1.json
def show
#comment = Comment.new(song: #song)
end
# GET /Songs/new
def new
#song = Song.new
end
# GET /Songs/1/edit
def edit
end
# POST /Songs
# POST /Songs.json
def create
#song = Song.new(song_params)
respond_to do |format|
if #song.save
format.html { redirect_to #song, notice: 'Song was successfully created.' }
format.json { render action: 'show', status: :created, location: #song }
else
format.html { render action: 'new' }
format.json { render json: #song.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /Songs/1
# PATCH/PUT /Songs/1.json
def update
respond_to do |format|
if #song.update(song_params)
format.html { redirect_to #song, notice: 'Song was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #song.errors, status: :unprocessable_entity }
end
end
end
# Song /Songs/1
# Song /Songs/1.json
def destroy
#song.destroy
respond_to do |format|
format.html { redirect_to songs_url }
format.json { head :no_content }
end
end
private
def set_song
#song = Song.find(params[:id])
end
def song_params
params.require(:song).permit(:title, :artist, :bio, :track, :user_id)
end
end
song.rb
class Song < ActiveRecord::Base
acts_as_voteable
belongs_to :user
has_many :comments, :dependent => :destroy
has_attached_file :track,
:url => "/assets/songs/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/songs/:id/:style/:basename.:extension"
validates_attachment :track, :presence => true
validates :title, length: { minimum: 10 }
validates :bio, length: { maximum: 300 }
end
user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :songs
has_many :comments
acts_as_voter
end
vote.rb
class Vote < ActiveRecord::Base
scope :for_voter, lambda { |*args| where(["voter_id = ? AND voter_type = ?", args.first.id, args.first.class.base_class.name]) }
scope :for_voteable, lambda { |*args| where(["voteable_id = ? AND voteable_type = ?", args.first.id, args.first.class.base_class.name]) }
scope :recent, lambda { |*args| where(["created_at > ?", (args.first || 2.weeks.ago)]) }
scope :descending, lambda { order("created_at DESC") }
belongs_to :voteable, :polymorphic => true
belongs_to :voter, :polymorphic => true
attr_accessible :vote, :voter, :voteable if ActiveRecord::VERSION::MAJOR < 4
# Comment out the line below to allow multiple votes per user.
validates_uniqueness_of :voteable_id, :scope => [:voteable_type, :voter_type, :voter_id]
end
routes.rb
Leap2::Application.routes.draw do
resources :comments
devise_for :users, controllers: {registrations: 'registrations'}
resources :songs
get '/contact', to: 'songs#contact'
get '/faq', to: 'songs#faq'
root to: 'songs#index'
end
Since you need an :id param, it should be get or put, and to avoid writing "song_songs" you can do
resources :songs do
member do
get :vote_for, :vote_against
end
end
That gets vote_for_song_path(#song) and vote_against_song_path(#song). Technically it would be more correct to have put :vote_for, since it's not an idempotent request, but then you'd need to remember to put method: :put at the end of your url.
Edit: to get the number of votes to show up, put an element in the page like
<span class="votes"><%= pluralize(song.votes.count, 'Vote') %></span>
right after the similar comments span. Also, since you have the vote_for_song_path called remotely, you'll want to update the page with js, which means you need to be able to find the spot on the page that has the number of votes for a song. Do do that, replace the <li> with
<%= content_tag_for :li, song do %>
[then put everything in your view that has to do with one song in the block]
<% end %>
That will generate html like <li id="song_21" class="song">..., which you can refer to in an update_votes.js.erb template,
$("#song_<%= #song.id %> .votes").html("<%= pluralize(song.votes.count, "Vote") %>")
That will send javascript to your view to update the proper element; you just have to instruct the controller to send it. I believe
format.js { render 'update_votes' }
in both the vote_for and vote_against actions will do it.

Resources