2016-03-18 56 views
0

我有Devise 3.5.6與正常的用戶模型。用戶關聯模型如user.user_profile如何在設計編輯表單中更新?

我創建了另一個型號爲user_profile,關係爲user has_one user_profileuser_profile belongs_to user

用戶配置包含:

create_table "user_profiles", force: :cascade do |t| 
    t.string "name" 
    t.string "city" 
    t.string "country" 
    t.text  "about" 
    t.integer "user_id" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
end 

現在我想的edit_user_registration_path有此模型的形式,領域以及該模型被保存編輯後的形式更新。

該模型在創建用戶後自動創建空字段。

+0

你可以使用'嵌套表格'來做這個 – Deep

+0

你能解釋一下嗎? –

回答

2

在你User模型,你需要寫:

accepts_nested_attributes_for :user_profile 

如果您正在使用rails 4然後在UsersController與其他強參數補充一點:

params.require(:user).permit(user_profile_attributes: [ :name, :city ]) 

然後在form你需要補充說明:

<%= f.fields_for :user_profile do |user_profile| %> 
    <%= user_profile.text_field :name %> 
    // The other fields 
<% end %> 

白名單列出您的所有個人資料屬性,當您提交user表格時,它也會提交關聯的個人資料屬性。

有各種寶石太當你要動態地添加字段,你可以使用它們:

希望這有助於。

+0

感謝這是我所需要的。非常感謝 –

相關問題