开发者

Customize Node Names in Ruby's Builder

开发者 https://www.devze.com 2022-12-16 05:11 出处:网络
I\'m building a tool that generates dynamic xml.As a result my Models have pretty generic names: Project

I'm building a tool that generates dynamic xml. As a result my Models have pretty generic names:

Project 
  has_many    :Groups

Group
  has_many    :Items
  has_many    :Groups
  belo开发者_运维技巧ngs_to  :Project

Item
  has_many    :Params
  belongs_to  :Group

Param
  belongs_to  :Project
  belongs_to  :Group
  belongs_to  :Item

So when I build the xml from the project controller, the project node name is the root node of the xml. But I don't want it called "project". I want the node to be whatever the @project.params['name'] value is.

The problem I'm having is that the structure of builder is making this difficult... When I do:

xml.project do
  ~some code
end

...It's always going to create "project" as the root node name. I can't find a way to override it to use a different name. I was hoping something like the following would work:

xml.send(@project.params.name) {
  ...some code
}

..but that obviously isn't working. So I'm essentially trying to find a way to alias the element names that are configured in my params model. Any suggestions would be greatly appreciated.


Instead of doing xml.project, try:

xml.tag! @project.params.name do

That should also be used if there is a hyphen in the element name.

0

精彩评论

暂无评论...
验证码 换一张
取 消