Note that there are some explanatory texts on larger screens.

plurals
  1. POIn Rails/Ruby how would I concat multiple form values into one model value?
    primarykey
    data
    text
    <p>I have a field in my model, :birth_time, that I'm trying to build from 3 form fields but am having a heck of a time. I've tried two different approaches. Here is a snippet from my form in my first approach:</p> <pre><code> &lt;input type="text" class="span1" name="birth_time_hour" id="birth_time_hour" placeholder="hour"&gt; &lt;input type="text" class="span1" name="birth_time_minute" id="birth_time_minute" placeholder="minute"&gt; &lt;select name="birth_time_meridiem" id="birth_time_meridiem" class="span1"&gt; &lt;option value="am"&gt;AM&lt;/option&gt; &lt;option value="pm"&gt;PM&lt;/option&gt; &lt;/select&gt; </code></pre> <p>And my model:</p> <pre><code>attr_accessible :birth_time </code></pre> <p>What I want to do in my controller is grab those three form values, create a Time object, and set it to Pick.birth_time before saving. I'm having a problem doing any arithmetic on params["birth_time_hour"]. If it's PM I want to add 12 to it before creating the Time object.</p> <p><code>params["birth_time_hour"] + 12</code> results in <code>can't convert Fixnum into String</code> <code>params["birth_time_hour"].to_i</code> results in <code>exception class/object expected</code>.</p> <p>Do I need to create an object from the form value first?</p> <p>My second approach was to add attr_accessible values to the model, even though they aren't actually stored in the database.</p> <pre><code>attr_accessible :birth_time_hour, :birth_time_minute, :birth_time_meridien, :birth_time </code></pre> <p>And then in my form (using simple_form):</p> <pre><code>&lt;%= f.input :birth_time, :label =&gt; "Birth time:", :as =&gt; :tel, :input_html =&gt; { :class =&gt; 'span2' }, :placeholder =&gt; 'hour' %&gt; </code></pre> <p>But that just bombs out when trying to render the form:</p> <pre><code>undefined method `birth_time_hour' for #&lt;Pick:0x0000010657c248&gt; </code></pre> <p>I'm not really sure why I can't have an attr_accessible for it. I've used it with password fields before that exist only on the form but not in the database.</p> <p>EDIT: It seems to work better when I use attr_accessible and attr_accessor for birth_time_hour. It can at least build the form but I still don't see it as part of the Pick object in the controller. I have very little knowledge of attr_accessor and what it's used for, that is clear I'm sure.</p> <p>Any ideas on how to solve a seemingly simple problem? I've read other SO threads that suggest storing all 3 values in the database, which seems stupid as all get out to me. I really hope that's not the case.</p>
    singulars
    1. This table or related slice is empty.
    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.
    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