加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 01:27:01 所属栏目:百科 来源:网络整理
导读:我想在rails中创建一个数字评级系统,用户可以在其中评分1到10的帖子. 我看过谷歌,但我只发现过时的教程和星级评级宝石,根本不能为我做这个工作. 也许有人可以指出我可以帮助我实现这一目标的宝石? 解决方法 Ruby Toolbox列出了几个,尽管大多数都是DOA. Mong
我想在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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读