我想在评论模型中添加5星的评分。我有一个表格:
<%= form_with(model: [ @product, @product.comments.build ], local: true) do |form| %>
<p>
<%= form.label t('activerecord.attributes.comment.commenter') %><br>
<%= form.text_field :commenter %>
</p>
<p>
<%= form.label t('activerecord.attributes.comment.body') %><br>
<%= form.text_area :body %>
</p>
<p>
<%= form.submit t('products.form.submit') %>
</p>
<% end %>
型号:
# Table name: comments
#
# id :bigint not null, primary key
# body :text
# commenter :string
# rating :float
# created_at :datetime not null
# updated_at :datetime not null
# product_id :bigint not null
我想添加一个字段:为此表单评分。
HTML:
<div id="reviewStars-input">
<input id="star-4" type="radio" value="5" name="reviewStars"/>
<label title="gorgeous" for="star-4"></label>
<input id="star-3" type="radio" value="4" name="reviewStars"/>
<label title="good" for="star-3"></label>
<input id="star-2" type="radio" value="3" name="reviewStars"/>
<label title="regular" for="star-2"></label>
<input id="star-1" type="radio" value="2" name="reviewStars"/>
<label title="poor" for="star-1"></label>
<input id="star-0" type="radio" value="1" name="reviewStars"/>
<label title="bad" for="star-0"></label>
</div>
如何使用辅助导轨,以便在模型字段中:等级是编号为1到5的条目
尝试以下操作:
class Comment < ApplicationRecord
STARS = [
[5, 'gorgeous'],
[4, 'good'],
[3, 'regular'],
...
]
和
<div id="reviewStars-input">
<%= collection_radio_buttons(:comment, :rating, Comment::STARS, :first, :second) do |b| %>
<%= b.radio_button %>
<%= b.label( title: b.text ) %>
<% end %>
</div>
将完全根据您需要的标签和类别而与您联系。