我是 RoR 的新手,我试图为每个特定网页上的产品设置一个向上/向下计数器。我正在努力弄清楚如何让计数器在页面上显示。
这是模型
class Rating < ActiveRecord::Base
def total(total)
@total = total
@like = Rating.find(:like, params[:id])
@dislike = Rating.find(:dislike, params[:id])
@like + @dislike = total
end
def average(average)
@average = average
average.to_f = (total / 2)
end
def overall(overall)
@overall = overall
if @like > average
overall = "More liked"
elsif @dislike > average
overall = "More disliked"
else
overall = nil
end
end
end
我的控制器
class RatingController < ApplicationController
def new
@rating = Rating.new
end
def index
@rating = Rating.all
end
def can_rate(user_sign)
@user_sign = user_sign
user_signed_in? == true ? (user_sign == true) : (user_sign = false)
end
def like(liked)
@liked = liked
if can_rate?
unless liked == true
liked = true
#Checks if dislike is true and subtracts it if it is
if disliked == true
Rating.decrement_counter(:dislike, params[:id])
disliked = false
end
Rating.increment_counter(:like, params[:id])
rating_id = Rating.find(params[:id])
rating_id.save
flash[:notice] = "You liked this!"
else
disliked = nil
liked = nil
Rating.decrement_counter(:like, params[:id])
rating_id.save
end
else
flash[:alert] = "You need to be signed in to use this feature!"
end
redirect_to :back
end
def dislike(disliked)
@disliked = disliked
if can_rate?
unless disliked == true
#Checks if dislike is true and subtracts it if it is
if liked = true
Rating.decrement_counter(:like, params[:id])
liked = false
end
disliked = true
Rating.increment_counter(:dislike, params[:id])
rating_id = Rating.find(params[:id])
rating_id.save
flash[:notice] = "You disliked this!"
else
disliked = nil
liked = nil
Rating.decrement_counter(:like, params[:id])
rating_id.save
end
else
flash[:alert] = "You need to be signed in to use this feature!"
end
redirect_to :back
end
end
我的索引(只是测试它是否有效)
<h1 style='text-align: center; border-bottom: 2px solid #ddd;'>Hello world!</h1>
<div>
<%= link_to 'like', :method => :like, :remote=>true %>
</div>
最后是路线
match '/rating', :to => 'rating#index', :as => :rating, via: 'get'
resources :rating
答案 0 :(得分:1)
评级控制器
Spree::Product.class_eval do
module Spree
class RatingsController < Spree::StoreController
def like
@product = Product.find(strong_prod_params)
if spree_current_user
if spree_current_user.voted_for?(@product) == false
@product.liked_by spree_current_user
registered
elsif spree_current_user.voted_up_on?(@product)
@product.unliked_by spree_current_user
elsif spree_current_user.voted_down_on?(@product)
@product.undisliked_by spree_current_user
@product.liked_by spree_current_user
registered
end
else
flash[:notice] = "You need to be signed in to use this feature!"
end
redirect_to :back
end
def dislike
@product = Product.find(strong_prod_params)
#if user is signed in
if spree_current_user
#self explanatory
if spree_current_user.voted_for?(@product) == false
@product.disliked_by spree_current_user
registered
elsif spree_current_user.voted_down_on?(@product)
@product.undisliked_by spree_current_user
elsif spree_current_user.voted_down_on?(@product)
@product.unliked_by spree_current_user
@product.disliked_by spree_current_user
registered
end
else
flash[:notice] = "You need to be signed in to use this feature!"
end
redirect_to :back
end
def registered
if @product.vote_registered?
flash[:notice] = "You have rated this!"
else
flash[:notice] = "Something went wrong!"
end
end
private
#just some cautionary strong parameters.
def strong_prod_params
params.require(:product).permit(:id)
end
end
end
end
处理模型和数据库在文档中。如果它不起作用,请尝试将 Product.find(strong_prod_params) 替换为 Product.find(params [:id]) 强> 这是我如何调用我喜欢的方法,以防你想知道
<%= link_to 'like', {:action => 'like', :controller => '/spree/ratings', :id => @product.id}, method: :put %>
这是我的路线
Spree::Core::Engine.routes.prepend do
put '/ratings' => 'ratings#like'
end