Note that there are some explanatory texts on larger screens.

plurals
  1. POOverride a form field value in rails form
    primarykey
    data
    text
    <p><strong>Note:</strong> I was overthinking things when I originally asked this question. The accepted answer is correct for the examples I gave - i.e. you can just pass <code>:value</code> to text_field, however I'd actually been having problems with <code>date_select</code>, which doesn't have a facility to override the value set.</p> <p>As a result this has now been updated in Rails, so you can set <code>:selected =&gt; a_date</code>, and it will work as expected. This will be in Rails 4.</p> <hr> <p>I have a model that can inherit its value from a parent model. It works something like this:</p> <pre><code>class User &lt; ActiveRecord::Base attr_accessible :field_name belongs_to :company def field_name if self['field_name'].nil? company['field_name'] else self['field_name'] end end end class Company &lt; ActiveRecord::Base attr_accessible :field_name end </code></pre> <p>I then have a form to edit the User, but of course, if the User value is nil, then it populates the form with the value from Company, which is not what I want.</p> <p>I would like to be able to override the value of the form field, so that if the User value is nil, then the value is empty.</p> <p><strong>Attempt 1</strong></p> <p>Ideally I'd be able to do:</p> <pre><code>&lt;%= form_for @user do |f| %&gt; &lt;%= f.text_field :field_name, @user['field_name'] %&gt; &lt;% end %&gt; </code></pre> <p>But that doesn't work, there doesn't seem to be a mechanism for providing an override value.</p> <p><strong>Attempt 2</strong></p> <p>So I thought about creating a second getter/setter:</p> <pre><code>def field_name_uninherited self['field_name'] end def field_name_uninherited=(value) self['field_name']=value end </code></pre> <p>Now I can use <code>&lt;%= f.text_field :field_name_uninherited %&gt;</code> and it works as expected - great! <strong>Except:</strong> when <code>field_name</code> is a date, or other type using multiparameter attributes, it results in this error:</p> <pre><code>1 error(s) on assignment of multiparameter attributes </code></pre> <p>I believe this is because it doesn't know that this is a date field, as it infers this from the database, and this field (with _uninherited suffix) is not in the database.</p> <p><em>So I need some way to mark my additional method as the same type as the original database field.</em></p> <hr> <p>A further note, the above examples (using field_name) are a simplified version. I'm actually using <a href="https://github.com/colinbm/inherits_values_from" rel="nofollow">https://github.com/colinbm/inherits_values_from</a> to handle the inheritance, but I don't think this is important to the question.</p> <hr> <p>Obviously if there's a better way to accomplish the same goal, then I'm all ears.</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.
    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