5

我不太确定正确的术语是什么,但我想做的是在一个表单中(最好使用 simple_form gem)有一个输入,:maximum,同时使用文本字段和选择框。用户将在文本框中键入一个数字,然后从小时、天或月的下拉框中进行选择。所以 21 天、3 个月、3 小时等。提交表单后,我会将其转换为天数并将其存储在数据库中。我知道如何更改 simple_form 中的输入类型,但是一个变量可以有两个输入吗?

4

1 回答 1

4

当然:)这是我的想法:

首先,您在用户模型中定义访问器:

attr_accessor :thing, :another_thing, :and_another_thing

然后在你看来,'inside' form_for helper,你可以这样写:

<%= form.input :thing, :as => :boolean %>
<%= form.input :another_thing, :as => :text %>

...或任何你想要的。(注意:我在formtastic这里使用。如果你不使用 formtastic gem,你应该考虑使用 Rails 方法。)

最后,在用户模型中定义一个回调:

before_create :build_my_fancy_record

def build_my_fancy_record
  self.storage_field = "#{thing} #{another_thing}"
end
于 2011-08-19T23:04:55.747 回答