2011-05-21 121 views
0

我想添加一個嵌入配置文件到用戶,我不斷收到此錯誤。Mongoid從視圖創建嵌入文檔

Access to the collection for Profile is not allowed since it is an embedded document, please access a collection from the root document. 

我相信這是一個簡單的問題來解決,但我不知道該怎麼做。我對RoR很新,所以事情還是有點混亂。這是我的代碼。

型號/配置文件

class Profile 
    include Mongoid::Document 
    attr_accessible :handle, :description 

    field :handle 
    field :description 
    embedded_in :user 
end 

控制器/簡介

class ProfileController < ApplicationController 
    def create 
    @user = current_user 
    @profile = @user.profile.create!(params[:profile]) 
    redirect_to dashboard_path 
    end 
end 

查看/資料/新

<h1>Create Profile</h1> 

<%= form_for [:current_user, Profile.create] do |f| %> 
<div class="field"> 
    <%= f.label :handle %> 
    <%= f.text_field :handle %> 
</div> 
<div class="field"> 
    <%= f.label :description %> 
    <%= f.text_area:description %> 
</div> 

    <p class="button"><%= f.submit %></p> 
<% end %> 

回答

0

嘗試

@user = current_user 
@profile = Profile.new(params[:profile]) 
@user.profile = @profile 
@user.save 
# or @profile.save 
+0

我收到同樣的錯誤消息。 – MattAitchison 2011-05-21 17:01:14

+0

用戶模型中是否有「embeds_one:profile」? – sandrew 2011-05-21 21:48:19

+0

是的。你想讓我發佈嗎? – MattAitchison 2011-05-22 02:47:11

1

您的views.html.erb中不能使用Profile.create,因爲Profile已嵌入到用戶中。所以,你需要做一些像current_user.build_profile

<%= form_for [:current_user, current_user.build_profile] do |f| %> 

應該工作