I have had a lot of questions lately, but thanks to this amazing community, I am learning a ton.
I got all the help I need with polymorphic associations before, and now I have a question about solving forms with polymorphic models. For example, I have Phoneable and User, so when I create my user registration form, I want to be able to assign several phone numbers to the user (for example, cell, work, home).
class User < ActiveRecord::Base has_many :phones, :as => :phoneable end class Phone < ActiveRecord::Base belongs_to :phoneable, :polymorphic => true end class CreateUsers < ActiveRecord::Migration t.column :name, :string t.references :phoneable, :polymorphic => true end class CreatePhones < ActiveRecord::Migration t.column :area_code, :integer t.column :number, :integer t.column :type, :string t.references :phoneable, :polymorphic => true end
Now, when I create my form, I am embarrassed. I usually did the following:
- form_for :user, :html => {:multipart => true} do |form| %fieldset %label{:for => "name"} Name: = form.text_field :name
Using polymorphism, I would just approach in the same way, but use fields_for?
- user_form.fields_for :phone do |phone| %> %label{for => "area_code"} Area Code: = phone.text_field :area_code %label{for => "number"} Number: = phone.text_field :number
Is this the right approach in this case?