Rails 3的应课差饷租模式 - 如何创建Ajax评价? [英] Rails 3 rateable model - How to create ajax rating?

查看:107
本文介绍了Rails 3的应课差饷租模式 - 如何创建Ajax评价?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建一些简单的Ajax评级好像有这个网页。 http://watir.com/documentation/ ?每位访客都应该能率,我不需要设置权限。我要存储在列的收视率。因此,用户可以通过的评分进行排序。 请做出相关详细例子。我不是一个JavaScript的专家。

How do I create some simple ajax rating like there is on this page http://watir.com/documentation/ ? Every visitor should be able to rate, I dont need to set permissions. I want to store the ratings in a column. So the user can sort by ratings. Please make an detailled example. I am not a javascript expert.

我发现一个例子来从头开始创建评级。但它授权用户。 有人能告诉我一个指导,共创收视率没有评估者(用户)?它不仅要存储的值,但也算的选票。

I have found an example to create ratings from scratch. But it authorizes a user. Can someone show me a guidance to create ratings without a Rater (user)? It should not only store the values but also count the votes.

<一个href="http://eighty-b.tumblr.com/post/1569674815/creating-an-ajaxified-star-rating-system-in-rails-3" rel="nofollow">http://eighty-b.tumblr.com/post/1569674815/creating-an-ajaxified-star-rating-system-in-rails-3

推荐答案

我最近做了一个简单的评价机制添加到现有的项目是以下内容:

What I did recently to add a simple rating mechanism to an existing project was the following:

我添加了两个领域现有的表(​​其中包含的项目进行评估)。这些都是:

I added two fields to an existing table (which contained the items to be rated). Those were:

rating_score => The current score
ratings => The number of ratings which led to the score

例如,如果有五个用户就已经评选为5为当前项目, rating_score 是25,和收视率是5,额定电流会计算为 rating_score /收视率

For example, if five users would've voted "5" for the current item, rating_score would be 25, and ratings would be 5. The current rating would be computed as rating_score / ratings.

然后,我增加了一个新的方法的项目将控制器评级,称为速度,这看起来是这样的:

Then I added a new method to the controller of the items to be rated, called "rate", which looked something like:

def rate
    @item = Item.find(params[:id])
    @container = "item"+@item.id.to_s

    @item.rating_score += params[:rating].to_i
    @item.ratings += 1
    @item.save

    respond_to do |format|
        format.js
    end
end

我认为该方法,叫做 rate.js.erb ,看起来像

$('#<%= @container %>').html('<%= escape_javascript(render(partial: 'rating', locals: { item: @item })) %>');

这code只有工作,如果你已经安装了jQuery的,但它应该是容易翻译为原型或任何JS框架,你可以使用。

This code works only if you've got jQuery installed, but it should be easily translatable to Prototype or whatever JS framework you may be using.

和href="http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials" rel="nofollow">部分的评级 _rating.html.erb ,是这样的:

And the partial for the rating, called _rating.html.erb, was something like:

<%= form_tag url_for(controller: 'items',  action: 'rate', id: item.id), remote: true %>
    <%= rating_stars(item.rating_score, item.ratings) %>
    <%= item.ratings %> Votes
</form>

在此部分中, RATING_STARS() helper方法产生某种明星般重新presentation的评级,但你可以做到这一点,只要你喜欢

In this partial, the rating_stars() helper method generated some kind of star-like representation for the rating, but you can do that however you like.

通过设置远程:真正的中的form_tag辅助,你的Rails安装会自动发送通过安装的javascript框架的要求。这神奇的是整个不显眼的JavaScript 东西Rails的事情最近,这实际上是pretty的凉爽。

By setting "remote: true" in the form_tag helper, your Rails installation should automatically transmit the request via the installed Javascript framework. This magic is part of the whole unobtrusive javascript thing going on in Rails lately, which is actually pretty cool.

希望这会为您提供了如何实现一个非常简单的评级系统的没有IP锁定功能,任何在Rails的一个想法。

Hope this gives you an idea of how to realize a very simple rating system with no IP lock feature whatsoever in Rails.

这篇关于Rails 3的应课差饷租模式 - 如何创建Ajax评价?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆