2016-10-30 33 views
0

我是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

你检查出[模板上轨导向(http://guides.rubyonrails.org/action_view_overview.html#templates-partials-and-layouts)? –

+0

嗨,欢迎来到堆栈溢出。我会指出几件小事,看看我们可以从那里去弄清楚什么是错的。首先:'@like + @dislike = total'我甚至不知道你在这里做什么......你的意思是@total = @ like.count - @ dislike.count'也许呢?即你是否试图储存总数?如果没有 - 那么我真的不明白,因为你不能给'@like + @ dislike'赋值 - 这是行不通的。你能解释一下你希望在那行代码中发生什么吗? –

+0

在索引操作中:'@rating = Rating.all'当你拿出一组东西(而不是一个)时,你应该使用一个复数变量名,例如在这里你应该使用'@ ratings' - 这是Rails的最佳实践,并且在将来可以更轻松地看到您拥有多个实例。 –

回答

1

我只用了一天与我以前acts_as_votable宝石来完成这项各种宝石左右摆弄之后。我使用的固相线作为发动机,这就是为什么它是如此难以作出一个从头开始,我本来试图做

等级控制器

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 [:编号]) 以下是我打电话给我的方法一样的情况下,你想知道

<%= link_to 'like', {:action => 'like', :controller => '/spree/ratings', :id => @product.id}, method: :put %> 

这里是我的路线

Spree::Core::Engine.routes.prepend do 
put '/ratings' => 'ratings#like' 
end 
相关问题