How to save form data to different models with one-to-one relationship in rails 3?

北慕城南 提交于 2019-12-13 20:22:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!