Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I went rooting around the Rails 3.1 source to sort this out, I figured that would be easier than searching anywhere else. Earlier versions of Rails should be similar. Jump to the end if tl;dr.</p> <hr> <p>When you call <code>form_for(@user)</code>, you end going through this:</p> <pre><code>def form_for(record, options = {}, &amp;proc) #... case record when String, Symbol object_name = record object = nil else object = record.is_a?(Array) ? record.last : record object_name = options[:as] || ActiveModel::Naming.param_key(object) apply_form_for_options!(record, options) end </code></pre> <p>And since <code>@user</code> is neither a String nor Object, you go through the <code>else</code> branch and into <code>apply_form_for_options!</code>. Inside <code>apply_form_for_options!</code> we see this:</p> <pre><code>as = options[:as] #... options[:html].reverse_merge!( :class =&gt; as ? "#{as}_#{action}" : dom_class(object, action), :id =&gt; as ? "#{as}_#{action}" : dom_id(object, action), :method =&gt; method ) </code></pre> <p>Pay attention to that chunk of code, it contains both the source of your problem and the solution. The <code>dom_id</code> method calls <code>record_key_for_dom_id</code> which looks like this:</p> <pre><code>def record_key_for_dom_id(record) record = record.to_model if record.respond_to?(:to_model) key = record.to_key key ? sanitize_dom_id(key.join('_')) : key end </code></pre> <p>And there's your call to <code>to_key</code>. The <code>to_key</code> method is defined by <a href="http://api.rubyonrails.org/classes/ActiveRecord/AttributeMethods/PrimaryKey.html"><code>ActiveRecord::AttributeMethods::PrimaryKey</code></a> and since you're not using ActiveRecord, you don't have a <code>to_key</code> method. If you have something in your model that behaves like a primary key then you could define your own <code>to_key</code> and leave it at that.</p> <p>But, if we go back to <code>apply_form_for_options!</code> we'll see another solution:</p> <pre><code>as = options[:as] </code></pre> <p>So you could supply the <code>:as</code> option to <code>form_for</code> to generate a DOM ID for your form by hand:</p> <pre><code>&lt;%= form_for(@user, :as =&gt; 'user_form') do |f| %&gt; </code></pre> <p>You'd have to make sure that the <code>:as</code> value was unique within the page though.</p> <hr> <p><strong>Executive Summary</strong>:</p> <ul> <li>If your model has an attribute that behaves like a primary key, then define your own <code>to_key</code> method that returns it.</li> <li>Or, supply an appropriate <code>:as</code> option to <code>form_for</code>.</li> </ul>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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