簡體   English   中英

在用戶模型中保存配置文件的嵌套屬性(Rails)

[英]Saving nested attributes for profile in user model (Rails)

我正在使用Devise gem在RoR中構建小頁面,並被卡住。 當我想通過用戶模型中的嵌套屬性保存配置文件時,出現錯誤,並且無法解決。

用戶模型

has_one :profile
accepts_nested_attributes_for :profile

輪廓模型

belongs_to :user

編輯用戶視圖(在這里我要添加個人資料信息)

<% resource.build_profile %>

<%= form_for(resource, :as => resource_name, 
            :url => registration_path(resource_name), 
            :html => { :method => :put, :class => "custom" }) do |f| %>

(some fields for user)

    <%= f.fields_for :profile, :child_index => resource.id, 
                     :html => {:class => "custom"} do |profile| %>

        <%= profile.label :name %>
        <%= profile.text_field :name %>

        <%= profile.label :surname %>
        <%= profile.text_field :surname %>

    <% end %>
    <%= f.submit "Update my profile!" %>
<% end %>

瀏覽器錯誤

Internal server error

終端錯誤

!! Unexpected error while processing request: expected Array (got Rack::Utils::KeySpaceConstrainedParams) for param `profile_attributes'

感謝您的建議:)

編輯

那是愚蠢的錯誤……我的表單中有這部分代碼:

                <%= profile.select :gender, %w[Male Female], {}, { :index => nil }%>

我刪除了{ :index => nil } ,現在問題消失了:)

也許您在user.rb文件中缺少此文件:

attr_accessible :profile_attributes

而且,如果它不起作用,請嘗試做

<% new_profile = resource.build_profile %>
...
<%= f.fields_for :profile, new_profile, ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM