【发布时间】:2020-05-05 05:55:22
【问题描述】:
我想为评论模型添加 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
我想在这个表单中添加一个字段 :rating 。
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 的条目
【问题讨论】:
-
注意
form.label t('activerecord.attributes.comment.body')也可以通过comment.human_attribute_name(:body)来实现。假设您首先将@product.comments.build保存到comment变量中。 -
谢谢,但这与我的问题无关
-
你应该使用 ruby 单选列表检查单选按钮的使用:stackoverflow.com/questions/52662473/…
标签: ruby-on-rails ruby ruby-on-rails-4 ruby-on-rails-5 helper