首先,我是一般的新手,而Rails是新手。我選擇了Rails,因爲它似乎是一種簡單的語言。對於我的項目,我正在使用MongoMapper和Rails。以Rails的形式處理MongoMapper嵌入式文檔
我正在嘗試使用與Document相同的格式處理嵌入式文檔。
我有以下型號:
class User
include MongoMapper::Document
key :email, String, :required => true
key :first_name, String
key :last_name, String
key :role, String
many :addresses
timestamps!
end
class Address
include MongoMapper::EmbeddedDocument
key :location, String
key :street, String
key :city, String
key :zip, Integer
key :state, String
key :country, String
end
我想創建/在同一時間作爲文檔編輯EmbeddedDocument。我一直在使用fields_for嘗試:
<% f.fields_for :address, @user.addresses do |address| -%>
<div class="field">
<%= address.label :street %><br />
<%= address.text_field :street %>
</div>
<% end %>
,但我得到
未定義的方法`大街」的#< \陣列:0x0000010126e3f8>
預先感謝您。
我不確定這是否有幫助,但通常這是通過在父模型中聲明「accep_nested_attributes_for」(在這種情況下爲User),然後在表單中提交包含「 _attributes」(address_attributes in這個案例)。 我知道Mongoid支持accept_nested_attributes_for。我不認爲MongoMapper會。一些谷歌搜索顯示其他一些人要求這個功能,無濟於事。 –
2011-06-14 20:55:38
請參閱此問題:http://stackoverflow.com/questions/2480165/creating-a-form-for-editing-embedded-documents-with-mongomapper – 2011-09-21 22:24:05