Rails 3 中将 xml 转换为 ActiveRecord 对象的内置方法?

发布于 2024-11-02 06:23:09 字数 291 浏览 1 评论 0原文

在 Rails 3 中,有没有一种方法可以从控制器中的 xml 生成 ActiveRecord 对象,而无需自己编写代码来显式解析它?例如,控制器可以接收 xml

<user>
 <first_name>Bob</first_name>
 <last_name>Smith</last_name>
</user>

并让它生成类似于 User.new(params[:user]) 的正确 User 对象吗?这是针对 api 的。

In Rails 3, is there a way to produce an ActiveRecord object from xml in a controller without writing code yourself to explicitly parse it? Say for example, could a controller receive xml like

<user>
 <first_name>Bob</first_name>
 <last_name>Smith</last_name>
</user>

and have it produce a proper User object similar to User.new(params[:user])? This is for an api.

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

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

发布评论

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

评论(2

知足的幸福 2024-11-09 06:23:09

是的,您可以这样做:

@user = User.new
@user.from_xml(xml_data)

更新

在覆盖时,您可以执行以下操作:

#user.rb
def from_xml(xml_data)
  book = Book.new
  book.from_xml(extract_xml_from(xml_data))
  self.books << book
  super(xml_data)
  save
  book.save
end

请注意,覆盖中最重要的行是 super(xml_data)将谨慎调用 ActiveRecord 模型的原始 from_xml(xml_data)
因此,您可以根据需要自定义其余部分,但如果您还想获得原始功能,则需要此行。
如果有不清楚的地方请告诉我。

Yes, you can do it like this:

@user = User.new
@user.from_xml(xml_data)

Update

On overriding you can do something like this:

#user.rb
def from_xml(xml_data)
  book = Book.new
  book.from_xml(extract_xml_from(xml_data))
  self.books << book
  super(xml_data)
  save
  book.save
end

Please note that the most important line in the overriding is the super(xml_data) which will take care on calling the original from_xml(xml_data) of the ActiveRecord model.
So you can customize the rest as needed, but this line is neede if you want to get the original functionality as well.
Let me know if something is not clear.

蓬勃野心 2024-11-09 06:23:09

我创建了一个 gem,xml_active 可以帮助您解决此问题,而无需编写大量代码。您可以在 https://rubygems.org/gems/xml_active 上查看。

要让它创建一个具有关联的对象,只需执行以下操作:

book = Book.one_from_xml xml_data

您还可以使用 xml_active 从 xml 创建许多对象以及关联。还有更多功能,但可能不在本答案的范围内。您可以在宝石的主页上查看它们。

更新

xml_active 现已正式退役,开发现在集中在 data_active 上(请参阅 https://github.com/michael -harrison/data_active),它具有 xml_active 的功能,但在未来的版本中我将努力支持其他格式

I've created a gem, xml_active that might help you with this without having to write a lot of code. You can check it out at https://rubygems.org/gems/xml_active.

To get it to create one object with associations just do the following:

book = Book.one_from_xml xml_data

You can also get xml_active to create many objects from xml along with associations. There are more features but probably not in the scope of this answer. You can check them out on the home page for the gem.

UPDATE

xml_active has now been officially retired and development is now focused on data_active (see https://github.com/michael-harrison/data_active) which has the functionality of xml_active but in future releases I will be working to support other formats

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