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,
:warningby default - Nil safe -
nilrenders nothing, so noifaround 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.
<%= 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.
<%= 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.
<%= 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
:sm
:base
:lg
:xl
<%= 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
<%= 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.
<%# 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
$640
Oak Shelf
$220
<%= 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.
| Name | Rating |
|---|---|
| Walnut Desk | |
| Oak Shelf | |
| Pine Stool |
<%= 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"andaria-label="4.5 out of 5". Every star icon isaria-hidden. - The visible value is
aria-hiddentoo, because the label already says it. The visiblelabel: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. |