我有一個用戶模型,定義一個has_one :profile
關聯。導軌形式助手更改形式text_field
在用戶編輯動作/頁面上,我更新用戶屬性。我想更新一個屬性不是屬於用戶型號,但是到配置文件一。
這是我的看法:
# _form.html.haml
=f.text_field :phone
使用這種形式,我怎麼可能還更新@user.profile
的phone
屬性?
我有一個用戶模型,定義一個has_one :profile
關聯。導軌形式助手更改形式text_field
在用戶編輯動作/頁面上,我更新用戶屬性。我想更新一個屬性不是屬於用戶型號,但是到配置文件一。
這是我的看法:
# _form.html.haml
=f.text_field :phone
使用這種形式,我怎麼可能還更新@user.profile
的phone
屬性?
作爲一般的想法,如果你有兩個表的用戶和配置文件與關聯然後 -
用戶表= ID,名字,姓氏
檔案表= ID,USER_ID,電話
協會:
User Class
-
class User < ActiveRecord::Base
has_one :profile
end
Profile Class
-
class Profile < ActiveRecord::Base
belongs_to :user
end
User Controller
-
def index
@user = current_user
@user_phone = @user.profile.phone
end
您應該允許嵌套屬性的更新您的用戶模式:
class User
has_one :profile
accepts_nested_attributes_for :profile
end
然後在您的形式使用fields_for
方法巢域從用戶形態:
= f.fields_for :profile do |p|
= p.text_field :phone
請說明用戶和配置文件表格及其關聯的相應字段之間的關係。 –
您需要Profile對象的嵌套表單。查看嵌套資源doc' – MrYoshiji