Rails 3和Jquery的rateit插件

时间:2013-02-18 10:58:56

标签: jquery ruby-on-rails ruby-on-rails-3

我想在我的食谱应用程序中添加一个星级评分系统,每个用户可以评价一次不同的食谱。我已经读过Jquery的评价,这是一个很好的起点。 http://www.radioactivethinking.com/rateit/example/example.htm我的问题是关于如何让它在rails 3中运行,我最初的想法是

设置一个看起来像这样的评级模型

class Rating < ActiveRecord::Base

has_many :recipes
has_many :users
attr_accessible :recipe_id, :user_id, :number  
validates_uniqueness_of :user_id, :scope => :recipe_id
end

设置模型关系

class Recipe < ActiveRecord::Base

has_many :ratings
end

class User < ActiveRecord::Base

has_many :ratings
end

控制器

class RatingsController < ApplicationController

 def new
 @rating = Rating.new
 end

 def create
 @rating = Rating.new(params[:rating])
  if @rating.save
    redirect_to my_recipes_path, :notice => "Thanks #{current_user.name} Recipe successfully Rated."
 end

如前所述,我正在考虑使用Jquery插件rateit,它可以使用HTML范围输入

<input type="range" min="0" max="5" value="0" step="0.5" id="backing2">
<div class="rateit" data-rateit-backingfld="#backing2"></div>

我的想法是,要向模型提交星级评级,我需要将值作为隐藏字段提交,类似于

<%= form_for @rating do |f| %>
<%= f.hidden_field :user_id, :value => current_user.id %>
<%= f.hidden_field :recipe_id, :value => recipe.id %>
<%= f.submit 'Rate Recipe' %>
<% end %>

我不确定的是如何将给定值的评级链接到模型中。理想情况下,我希望通过Ajax请求实现此目的。

我的关联可能不正确,因为很多我的代码可能?在我希望有人能指出我正确的指导或至少指出我目前的错误之前,从未接触过这样的事情

由于

1 个答案:

答案 0 :(得分:2)

如何使用The Rateit Gem

https://github.com/ouvrages/rails-rateit

我没有使用过它,只是为了“Rateit Gem”做了谷歌,这是最受欢迎的。