以 Rails 形式处理 MongoMapper EmbeddedDocument

发布于 2024-11-03 20:01:02 字数 961 浏览 3 评论 0原文

首先,我对一般编程和 Rails 都是新手。我选择 Rails 是因为它看起来是一种很容易上手的语言。对于我的项目,我将 MongoMapper 与 Rails 结合使用。

我正在尝试以与文档相同的形式处理嵌入文档。

我有以下模型:

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

我想在创建/编辑文档的同时创建/编辑嵌入文档。我尝试过使用 fields_for:

<% f.fields_for :address, @user.addresses do |address| -%>
  <div class="field">
    <%= address.label :street %><br />
    <%= address.text_field :street %>
  </div>
<% end %>   

但我得到

#<\Array:0x0000010126e3f8> 的未定义方法“street”

先感谢您。

First of all, I'm new to programming in general and new to Rails. I picked up Rails because it seems an easy language to start with. For my project I'm using MongoMapper with Rails.

I'm trying to process an Embedded Document in the same form as the Document.

I have the following model:

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

I want to create/edit the EmbeddedDocument at the same time as the Document. I have tried using fields_for:

<% f.fields_for :address, @user.addresses do |address| -%>
  <div class="field">
    <%= address.label :street %><br />
    <%= address.text_field :street %>
  </div>
<% end %>   

But I get

undefined method `street' for #<\Array:0x0000010126e3f8>

Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

—━☆沉默づ 2024-11-10 20:01:02

如果您在 User 上定义 addresses_attributes=fields_for 将起作用。由于某种原因,如果您定义该方法,fields_for 实际上会更改其行为。

这是一个示例实现:

def addresses_attributes=(id_and_attrs)    
  id_and_attrs.each do |id, attrs|
    address = self.addresses.find(id) || self.addresses.build(:id => id)
    address.attributes = attrs
  end
end

未经测试,因此您必须解决问题。特别是,如果地址是全新的,因为用户是新的,我不记得嵌套参数如何以不同的方式通过。

fields_for will work if you define addresses_attributes= on User. For some reason fields_for actually changes its behavior if you define that method.

Here's an example implementation:

def addresses_attributes=(id_and_attrs)    
  id_and_attrs.each do |id, attrs|
    address = self.addresses.find(id) || self.addresses.build(:id => id)
    address.attributes = attrs
  end
end

Untested, so you'll have to work out the kinks. In particular, I don't remember how the nested parameters come through differently if the addresses are completely new because the user is new.

酷炫老祖宗 2024-11-10 20:01:02

据我记得 fields_for 不包含对象的 id,为此您需要添加一个隐藏字段。

<%= address.hidden_field :id, :value => address.object.id %>

那么你的addresses_attributes =方法将如下所示:

def addresses_attributes=(addresses_attributes)    
  addresses_attributes.each do |index, attrs|
    address = self.addresses.find(attrs['id']) || self.addresses.build
    address.attributes = attrs
  end
end

谢谢Brian,这为我遇到的类似问题提供了解决方案。

As far as I remember fields_for does not include the id of the object, for that you will need to add a hidden field.

<%= address.hidden_field :id, :value => address.object.id %>

Then your addresses_attributes= method would look like this:

def addresses_attributes=(addresses_attributes)    
  addresses_attributes.each do |index, attrs|
    address = self.addresses.find(attrs['id']) || self.addresses.build
    address.attributes = attrs
  end
end

Thanks Brian, this provide a solution to a similar problem I was having.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文