Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Why not use the time_select? It does all this automatically for you. <a href="http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-time_select" rel="nofollow noreferrer">http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-time_select</a></p> <pre><code>attr_accessible :birth_time </code></pre> <p>is all you need in your model.</p> <p>In your form use</p> <pre><code> &lt;%= f.time_select :birth_time %&gt; </code></pre> <p>time_select has a few options. Refer the manual.</p> <hr> <p>You could use <code>composed_of</code> <a href="https://stackoverflow.com/questions/2718367/composed-of-in-rails-when-to-use-it">composed_of in Rails - when to use it?</a></p> <hr> <p>If that's not good enough, then... I guess you can just create your own setter method. (To answer your question) </p> <pre><code>def birth_time=(birth_time) # create the time object from birth_time end </code></pre> <p>In your form</p> <pre><code>= text_field_tag "model_name[birth_time][hr]" = text_field_tag "model_name[birth_time][min]" = text_field_tag "model_name[birth_time][sec]" </code></pre> <p>(you could also use <code>f.fields_for :birth_time</code> if you would prefer that method of generating the names)</p> <pre><code>= form_for @model do |f| = f.fields_for :birth_time do |btf| - [:hr, :min, :sec].each do |n| = btf.text_field n </code></pre> <p>Now, In your setter method, you will get a hash that looks like this:</p> <pre><code>{ 'hr' =&gt; '12', 'min' =&gt; '16', 'sec' =&gt; '26' } </code></pre> <p>With that, you can construct your value from the hash and set it to:</p> <pre><code>self[:birth_time] = </code></pre> <p>I hope that helps. There might be other ways to do it, but this, I think is the simplest. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload