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
在Rails 3中,有没有办法在控制器中从xml生成ActiveRecord对象而无需自己编写代码来显式解析它?比方说,控制器是否可以接收xml
<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.
并生成一个类似于User.new(params [:user])的正确User对象?这是一个api。
2 个解决方案
#1
6
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.
请注意,覆盖中最重要的一行是super(xml_data),它将负责调用ActiveRecord模型的原始from_xml(xml_data)。因此,您可以根据需要自定义其余部分,但如果您还想获得原始功能,则必须使用此行。如果有什么不清楚,请告诉我。
#2
2
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.
我创建了一个gem,xml_active可以帮助你完成这个,而不必编写大量的代码。您可以访问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.
您还可以获取xml_active以从xml创建许多对象以及关联。有更多功能,但可能不在本答案的范围内。您可以在主页上查看宝石。
UPDATE
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
xml_active现已正式退役,开发现在专注于data_active(请参阅https://github.com/michael-harrison/data_active),它具有xml_active的功能,但在将来的版本中,我将努力支持其他格式
#1
6
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.
请注意,覆盖中最重要的一行是super(xml_data),它将负责调用ActiveRecord模型的原始from_xml(xml_data)。因此,您可以根据需要自定义其余部分,但如果您还想获得原始功能,则必须使用此行。如果有什么不清楚,请告诉我。
#2
2
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.
我创建了一个gem,xml_active可以帮助你完成这个,而不必编写大量的代码。您可以访问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.
您还可以获取xml_active以从xml创建许多对象以及关联。有更多功能,但可能不在本答案的范围内。您可以在主页上查看宝石。
UPDATE
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
xml_active现已正式退役,开发现在专注于data_active(请参阅https://github.com/michael-harrison/data_active),它具有xml_active的功能,但在将来的版本中,我将努力支持其他格式