5
votes

I'm not quite sure what the correct terms are, but what I'm trying to do is in a form (preferably using the simple_form gem) have one of the inputs, :maximum, use both a text field and select box. The user would type in the text box a number, and then select from a dropdown box of hours, days, or months. So 21 days, 3 months, 3 hours, etc. When the form was submitted I would convert that to days and store it in the database. I know how to change the input type in simple_form, but is it possible to have two inputs for one variable?

1
I guess you could use attr_accessors and then process logic later on, in a callback like before_save.socjopata
Could you elaborate on how attr_accessor would be used?Alexei

1 Answers

4
votes

Sure :) Here is my idea:

First, you define accessors in your user model:

attr_accessor :thing, :another_thing, :and_another_thing

Then in your view, 'inside' form_for helper, you could write for example:

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

...or whatever you want. (Note: I am using formtastic here. You should consider using Rails methods if you're not using formtastic gem. )

Finally, you define a callback in you user model:

before_create :build_my_fancy_record

def build_my_fancy_record
  self.storage_field = "#{thing} #{another_thing}"
end