2012-01-15 61 views
3

我想創建一個數字評分系統在用戶可以評分從1 - 10的職位。如何在Rails中創建數字評分系統?

我在谷歌上看過,但我只找到過時的教程和星級評定寶石,根本沒有爲我做這項工作。

也許有人可以指給我一個可以幫助我實現這個目標的寶石?

回答

6

Ruby Toolbox列出了幾個,儘管大多數都是DOA。 Mongoid_ratings似乎是最近更新的,儘管你可能不想去Mongo路線。

https://www.ruby-toolbox.com/categories/rails_ratings

我會建議從頭開始構建。下面有一個快速的(可能非功能性/非安全)黑客可能會幫助你開始:

路線

resources :articles do 
    resources :ratings 
end 

模式

class Article < ActiveRecord::Base 
    has_many :ratings, :dependent => :destroy 
end 

class Rating < ActiveRecord::Base 
    belongs_to :article 
    validates_presence_of :article 
    validates_inclusion_of :value, :in => 1..10 
end 

控制器

class RatingsController < ApplicationController 
    before_filter :set_article 

    def create 
    @rating = @article.ratings.new :value => params[:value] 
    if @rating.save 
     redirect_to article_ratings_path(@article), :notice => "Rating successful." 
    else 
     redirect_to article_ratings_path(@article), :notice => "Something went wrong." 
    end 
    end 

    def update 
    @rating = Rating.find(params[:id]) 
    @rating.update_attribute :value, params[:value] 
    end 

    private 
    def set_article 
     @article = Article.find(parms[:article_id]) 
    end 
end 

在某處的一篇文章的觀點:

form_for [@article,@rating] do |f| 
    f.select("rating", "value", (1..10)) 
    f.submit "Rate this Article" 
end 
+0

嘿男人!非常感謝您的詳細解答。關鍵是我也和用戶一起工作,我只需要用戶能夠對文章評分一次。我將如何能夠做到這一點? – imjp 2012-01-16 03:20:57

+1

一個複雜的問題,http://railscasts.com/episodes/209-introducing-devise可能會讓你開始 – 2012-01-16 20:45:50