问题
I have a user controller and a user model. In my user table I have limited fields. But now I want to create separate tables for user bank info and user personal info and save through the one form only. How is it possible, I am sure their must be something for this problem?
回答1:
Check how nested_forms works along with the view helpers.
Basically you add this in your user class :
class User < AR
has_one :profile
has_one :address
accepts_nested_attributes_for :profile, :adress
attr_accessible :name, :email, :profile_attributes, address_attributes #etc
end
and in you form :
=form_for @user do |user_form|
= user_form.text_field :name
= user_form.field_for :profile do |profile_form|
=profile_form.text_field :bank_name
= user_form.field_for :address do |address_form|
=address_form.text_field :city
来源:https://stackoverflow.com/questions/7660023/how-to-save-form-data-to-different-models-with-one-to-one-relationship-in-rails