Note that there are some explanatory texts on larger screens.

plurals
  1. POSet fields in a controller
    primarykey
    data
    text
    <p>When creating a new Person, how do I set its fields that are not included in the new.html.erb form?</p> <p>Here is the controller and form:</p> <pre><code>class PeopleController &lt; ApplicationController def new @account = Account.find_by_id(params[:account_id]) organization = @account.organizations.primary.first location = organization.locations.primary.first @person = location.persons.build end def create @person = Person.new(params[:person]) if @person.save flash[:success] = "Person added successfully" redirect_to account_path(params[:account_id]) else render 'new' end end end &lt;h2&gt;Account: &lt;%= @account.organizations.primary.first.name %&gt;&lt;/h2&gt; &lt;%= form_for @person do |f| %&gt; &lt;%= f.label :first_name %&gt;&lt;br /&gt; &lt;%= f.text_field :first_name %&gt;&lt;br /&gt; &lt;%= f.label :last_name %&gt;&lt;br /&gt; &lt;%= f.text_field :last_name %&gt;&lt;br /&gt; &lt;%= f.label :email1 %&gt;&lt;br /&gt; &lt;%= f.text_field :email1 %&gt;&lt;br /&gt; &lt;%= f.label :home_phone %&gt;&lt;br /&gt; &lt;%= f.text_field :home_phone %&gt;&lt;br /&gt; &lt;%= f.submit "Add person" %&gt; &lt;% end %&gt; </code></pre> <p>Here are the models:</p> <pre><code>class Location &lt; ActiveRecord::Base belongs_to :organization has_many :persons, :as =&gt; :linkable has_one :address, :as =&gt; :addressable scope :primary, where('locations.primary_location = ?', true) accepts_nested_attributes_for :address end class Person &lt; ActiveRecord::Base belongs_to :linkable, :polymorphic =&gt; true end </code></pre> <p>The association method @person = location.persons.build works fine in the Rails Console. It sets the 'linkable_id' field to 1 and the 'linkable_type' field to 'Location'. However, after submitting the form the Person is created but these two fields are left blank.</p> <p>Any help with this problem will be greatly appreciated.</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