当前位置 : 主页 > 编程语言 > ruby >

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

来源:互联网 收集:自由互联 发布时间:2021-06-23
我想在rails中创建一个数字评级系统,用户可以在其中评分1到10的帖子. 我看过谷歌,但我只发现过时的教程和星级评级宝石,根本不能为我做这个工作. 也许有人可以指出我可以帮助我实现
我想在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
网友评论