ruby-on-rails – 如何在Rails中创建数字评级系统?

我想在rails中创建一个数字评级系统,用户可以在其中评分1到10的帖子.

我看过谷歌,但我只发现过时的教程和星级评级宝石,根本不能为我做这个工作.

也许有人可以指出我可以帮助我实现这一目标的宝石?

解决方法

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

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...