Rapid Rails UI Pro

—

Upgrade to unlock this component

Get Pro →

Rating

Read-only star rating with full, half and empty stars, an optional value and a label. New in 0.96.0.

Key Features

  • Half stars - Values round to the nearest 0.5
  • Up to 10 stars - max: from 1 to 10
  • Value and label - Show "4.5" and "128 reviews" next to the stars
  • 5 sizes - xs, sm, base, lg, xl
  • Any colour - Semantic or Tailwind colours, :warning by default
  • Nil safe - nil renders nothing, so no if around it
  • Accessible - One image role labelled "4.5 out of 5"
  • Zero JavaScript - Pure Ruby and CSS

Basic Usage

Pass the rating as the first argument.

Basic
<%= rui_rating(4) %>
<%= rui_rating(4.5) %>
<%= rui_rating(product.average_rating) %>

Half Stars & Rounding

Values round to the nearest half and clamp to 0..max. Numeric strings work too, so a decimal column can be passed straight through.

Value Renders
3.2
3.3
3.7
"4.25"
0
7

A half star clips a filled star to its start half, so it mirrors correctly in right to left layouts.

Value & Label

show_value: true prints the number after the stars. label: adds visible text after that.

128 reviews
128 reviews
Value and label
<%= rui_rating(4.5, show_value: true) %>
<%= rui_rating(4.5, label: "128 reviews") %>
<%= rui_rating(product.average_rating,
               show_value: true,
               label: pluralize(product.reviews_count, "review")) %>

Max Stars

max: sets the number of stars, from 1 to 10. The default is 5.

max:
<%= rui_rating(7, max: 10, show_value: true) %>
<%= rui_rating(2.5, max: 3) %>

Sizes

Five sizes scale the stars, the value and the label together.

:xs
128 reviews
:sm
128 reviews
:base
128 reviews
:lg
128 reviews
:xl
128 reviews
size:
<%= rui_rating(4.5, size: :xs) %>
<%= rui_rating(4.5, size: :sm) %>
<%= rui_rating(4.5) %>             <%# :base %>
<%= rui_rating(4.5, size: :lg) %>
<%= rui_rating(4.5, size: :xl) %>

Colors

color: tints the filled stars. It takes a semantic name or any Tailwind colour. Empty stars stay neutral in light and dark mode.

:warning
:danger
:success
:primary
:emerald
:violet
color:
<%= rui_rating(3.5) %>                   <%# :warning (default) %>
<%= rui_rating(3.5, color: :danger) %>
<%= rui_rating(3.5, color: :emerald) %>

Nil Values

A nil value renders nothing, so a record with no ratings yet needs no conditional.

nil renders nothing
<%# No reviews yet: average_rating is nil, nothing is rendered %>
<%= rui_rating(product.average_rating, label: "#{product.reviews_count} reviews") %>

Common Patterns

Product card

Walnut Desk

(212)

$640

Oak Shelf

(48)

$220

Product card
<%= rui_card do |card| %>
  <% card.with_body do %>
    <%= rui_text(product.name, weight: :semibold) %>
    <%= rui_rating(product.average_rating, size: :sm,
                   show_value: true, label: "(#{product.reviews_count})") %>
  <% end %>
<% end %>

Table column

Pass a block to the column. A product with no rating gets an empty cell.

NameRating
Walnut Desk
Oak Shelf
Pine Stool
In rui_table
<%= rui_table(@products) do |table| %>
  <% table.column(:name) %>
  <% table.column(:average_rating, label: "Rating") { |product| rui_rating(product.average_rating, size: :xs) } %>
<% end %>

Accessibility

  • The stars are one element with role="img" and aria-label="4.5 out of 5". Every star icon is aria-hidden.
  • The visible value is aria-hidden too, because the label already says it. The visible label: is read normally.
  • Pass aria_label: to change the spoken text, e.g. "Rated 4.5 by 128 buyers".

Read-only: this component displays a rating. An interactive rating input for forms is not part of it yet.

API Reference

rui_rating

Read-only star rating with full, half and empty stars. No JavaScript.

Parameter Type Default Description
value Numeric or nil — Positional. Numeric or numeric string; rounds to the nearest 0.5 and clamps to 0..max. nil renders nothing.
max Integer 5 Number of stars, 1 to 10.
color Symbol :warning Filled star colour. Semantic name or any Tailwind color.
size Symbol :base Star and text size
:xs :sm :base :lg :xl
show_value Boolean false Show the number (e.g. 4.5) after the stars.
label String — Visible text after the stars, e.g. "128 reviews".
aria_label String — Overrides the accessible label, "4.5 out of 5".
class String — Wrapper classes merged via tailwind_merge. Other HTML attributes (id:, data:) pass through.

Related Components