【问题标题】:Rails Simple Form :first :lastRails 简单形式 :first :last
【发布时间】:2018-04-11 21:40:51
【问题描述】:

使用简单表单 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 是什么?他们有必要吗?我发现解释 :first 和 :last 的文档很少。

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

【问题讨论】:

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


    【解决方案1】:

    在您的情况下,它们是必要的,firstlast 是数组方法。 (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'
    

    【讨论】:

    • 因此 :shower_flow_rate 将注册为属性,而 :first 是将数组中的第一个值存储为属性的方法。为什么我需要 :last 那么如果我只想显示“是”但从不保存?
    • 是的,首先,即2.5 是单选按钮的值,而Yes 将是该单选按钮的标签。换句话说,每当您选择带有标签Yes 的单选按钮时,它的值2.5 将在参数中发送。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-21
    • 1970-01-01
    • 2015-11-04
    • 1970-01-01
    • 2020-10-11
    • 2011-01-22
    相关资源
    最近更新 更多