1

我没有将此功能视为插件,并且想知道如何实现它,希望使用rails。

我追求的功能是能够在一页/表格上通过各种属性(例如情节、娱乐性、原创性等)对一个对象(一部电影)进行评分。

任何人都可以帮忙吗?

4

3 回答 3

1

我认为您不需要插件来做到这一点...您可以使用 AR 执行以下操作

class Film < ActiveRecord::Base
  has_many :ratings, :as => :rateable

  def rating_for(field)
    ratings.find_by_name(field).value
  end

  def rating_for=(field, value)
    rating = nil
    begin
      rating = ratigns.find_by_name(field)
      if rating.nil?
        rating = Rating.create!(:rateable => self, :name => field, :value => value)
      else
        rating.update_attributes!(:value => value)
      end
    rescue ActiveRecord::RecordInvalid
      self.errors.add_to_base(rating.errors.full_messages.join("\n"))
    end
  end

end

class Rating < ActiveRecord::Base
  # Has the following field:
  # column :rateable_id, Integer
  # column :name, String
  # column :value, Integer
  belongs_to :rateable, :polymorphic => true
  validates_inclusion_of :value, :in => (1..5)
  validates_uniqueness_of :name, :scope => :rateable_id

end

当然,使用这种方法,您将在 Rating 名称中进行复制,这并不是那么糟糕(仅针对一个字段规范化表并不会削减它)。

于 2010-02-01T19:33:56.500 回答
0

您还可以使用插件ajaxfull-rating

于 2010-02-01T21:28:39.233 回答
0

这是另一个可能更强大的评级插件......它已经存在了一段时间,并且已经过修改以与 Rails 2 一起使用

http://agilewebdevelopment.com/plugins/acts_as_rateable

于 2010-02-03T03:39:49.497 回答