Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simple_form input with multiple fields

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?

like image 276
Alexei Avatar asked Oct 10 '22 05:10

Alexei


1 Answers

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
like image 108
socjopata Avatar answered Oct 12 '22 23:10

socjopata