Suppose I have the following models/migrations:
class Car < ActiveRecord::Base belongs_to :manufacturer validates_presence_of :manufacturer validates_association :manufacturer end
class CreateCars < ActiveRecord::Migration def self.up create_table cars do |t| t.column :name, :string t.column :manufacturer_id, :integer end end
class Manufacturer < ActiveRecord::Base has_many :cars end
class CreateManufacturers < ActiveRecord::Migration def self.up create_table maufacturers do |t| t.column :name, :string end end
Now, I need a form to create new Cars that has two text fields: name, and manufacturer. I can't use a select for manufacturer, because there are far too many of them (let's pretend). What I can't figure out is how to construct the text_field helper tag in my form such that Rails understands that the form field doesn't map directly to a field in the Car object, but rather to the name field of the car's Manufacturer object. Is there even a way to do it? I'm looking for something like one of these (neither of which work, obviously, but they may help to clarify what I'm trying to do):
<%= text_field 'car', 'car.manufacturer.name' %> or <%= text_field 'car', 'car.manufacturer[:name]' %>
Without the ability to do that, I'm not sure how I could have - for example - an edit form without writing special code to populate that field, or how any of the validates_* methods would work. I've tried using fields_for, but it doesn't work; probably because I'm not doing it right. Maybe someone knows how to use fields_for to accomplish the above? I think what I tried was something like this:
<% fields_for :manufacturer, @car.manufacturer do |m_field| %> <%= m_field.text_field :name %> <% end %>