Rails简单形式:第一个:最后一个

时间:2018-04-11 21:40:52

标签: ruby-on-rails database simple-form

使用简单形式的gem编写一个相当长的表单,但是单选按钮集合有问题。向用户询问问题并具有可选择的单选按钮,但输入的值与为用户显示的值不同。这是我的代码的示例行,

  <%= f.collection_radio_buttons :shower_flow_rate, prompt: "Do you have low flow shower heads?",[[2.5, 'Yes'] ,[3.8, 'Some'], [5.0, 'No']], :first, :last%>

:shower_flow_rate是我想要填充的变量。什么是:第一个和最后一个?他们有必要吗?我发现很少有文档解释:first和:last。

此外,我在同一模式中有几个属性需要通过计算表单中某些输入的总和或乘积来填充。这些值应该在视图中还是在控制器中计算?

1 个答案:

答案 0 :(得分:0)

在你的情况下是的,他们是必要的,first&amp; last是数组方法。 (You can use any methods instead of :first, :last which is valid for the each element of the collection) 参考collection_radio_buttons

以下是集合

[[2.5, 'Yes'] ,[3.8, 'Some'], [5.0, 'No']]
So, :first & :last are the methods which will call on each 
element of the collection to create an HTML
# For Ex:- 
# [2.5, 'Yes'].first => 2.5
# [2.5, 'Yes'].last => 'Yes'