[英]Rails nesting params in controller create action
我正在開發一個rails項目,我正在嘗試添加一個新模型user_information並將其鏈接到用戶模型。
當我嘗試在user_controller中設置create函數時,我正在努力學習如何從user_information添加信息並將其作為參數傳遞給用戶對象。
有關嵌套這些參數的任何建議都非常有用
聽起來你正在尋找嵌套屬性( http://guides.rubyonrails.org/form_helpers.html#nested-forms )。
模型:
class User < ActiveRecord::Base
has_one :user_information
accepts_nested_parameters_for :user_information
class UserInformation < ActiveRecord::Base
belongs_to :user
現在,您可以通過嵌套參數在一個步驟中創建子關聯:
User.create(email: "<email>", user_information_attributes: { name: "john", etc.. })
(注意_attributes
后綴)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.