Rails ajax rating 帮助如何创建一个帮助方法来显示星星 [英] Rails ajax rating help how to create a helper method to display stars

查看:53
本文介绍了Rails ajax rating 帮助如何创建一个帮助方法来显示星星的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一些简单的 Ajax 评级.基于这个问题的第一个答案 Rails 3 rateable model - How创建 ajax 评级?

评级的部分,称为_rating.html.erb,类似于:

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

在这个部分中,rating_stars()辅助方法生成了某种星状表示评级,但你可以这样做你喜欢.

如何创建 rating_stars 辅助方法来显示星星?

解决方案

我建议你看看使用 jQuery RateIt 插件.

它完全是客户端 JavaScript 和 CSS.它还带有一些甜美的不引人注目的 JavaScript 和渐进式增强功能.我对我的项目很满意.

一旦您将 jquery.rateit.min.js、rateit.css、delete.gif 和 star.gif 包含到您的公共目录中,您的 rails 表单中可能会包含以下内容:

<%= f.text_field :ratings, :size =>1, :min =>0, :max =>5, :step =>1%><div class="rateit" data-rateit-backingfld="#item_ratings" data-rateit-resetable="true">

data-rateit-backingfld 中的 ID 应与 text_field 表单助手为您的项目字段生成的 ID 匹配.

显然,这是使用 HTML5 数据属性,但如果您使用的是 Rails 3,那么默认情况下您将使用 HTML5.

I am creating, some simple Ajax rating. Based on the first answer on this question Rails 3 rateable model - How to create ajax rating?

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>

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.

How do I create the rating_stars helper method to display the stars?

解决方案

I would suggest you have a look at using the jQuery RateIt plugin.

It is entirely client side JavaScript and CSS. It also sports some sweet unobtrusive JavaScript and progressive enhancement. I've been pleased with it in my projects.

Once you've included jquery.rateit.min.js, rateit.css, delete.gif and star.gif into your public directories, you might have something like the following inside your rails form:

<%= f.text_field :ratings, :size => 1, :min => 0, :max => 5, :step => 1 %>
<div class="rateit" data-rateit-backingfld="#item_ratings" data-rateit-resetable="true">
</div>

The ID in data-rateit-backingfld should match the ID produced by the text_field form helper for your items field.

Clearly, this is using HTML5 data attributes, but if you're using Rails 3, then you'll be using HTML5 by default.

这篇关于Rails ajax rating 帮助如何创建一个帮助方法来显示星星的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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