如何在Rails中构建一个灵活的数字评级功能系统?

2026-04-11 17:371阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计384个文字,预计阅读时间需要2分钟。

如何在Rails中构建一个灵活的数字评级功能系统?

我想在Rails中创建一个数字评级系统,用户可以在其中评分为1到10的帖子。我看过谷歌搜索,但只发现过时的教程和星级评级宝石,基本无法指导我完成这个工作。也许有人能指出我可以求助的资源或提供帮助实现这个功能。

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

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

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

Ruby Toolbox列出了几个,尽管大多数都是DOA. Mongoid_ratings似乎是最近更新的,尽管你可能不想去Mongo路线.

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

控制器

如何在Rails中构建一个灵活的数字评级功能系统?

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

本文共计384个文字,预计阅读时间需要2分钟。

如何在Rails中构建一个灵活的数字评级功能系统?

我想在Rails中创建一个数字评级系统,用户可以在其中评分为1到10的帖子。我看过谷歌搜索,但只发现过时的教程和星级评级宝石,基本无法指导我完成这个工作。也许有人能指出我可以求助的资源或提供帮助实现这个功能。

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

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

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

Ruby Toolbox列出了几个,尽管大多数都是DOA. Mongoid_ratings似乎是最近更新的,尽管你可能不想去Mongo路线.

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

控制器

如何在Rails中构建一个灵活的数字评级功能系统?

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