Saturday, 15 January 2011

Rails XML Builder - Code refactoring -


To generate XML, I have written the following code in my rail app. I use Aptana IDE for railway development I have been and the IDE shows a warning that code structure is similar in both blocks. What code changes can be made to remove duplicates in the structure? Is there any other way of writing this?

  xml.roles do @ rolesList.each do | R | Xml.role (: id = & gt; r ["role_id"] ,: name = & gt; r ["role_name"]) and end xml.levels do @ levelsList.each do | Lvl | Xml.level (: id = & gt; lvl ["level_id"] ,: name => lvl ["level_name"]) end  

Use the method of sending me and & lt; Send: id & gt; 12 & lt; / Send: id & gt; There was only one problem with tags appearing like . To solve, I "tag!" way. So I think your code will look like this:

  def build_xml (node_name, node_lit) xml.tag! (Node_name.plicate) node_list.Each | Node | Id_str = node ["# {node_name} _id"] "" {{node_name} _name "] xml.tag! (Nod_name, id = & id; id_str ,: name = & gt; name_str) and end and end  

No comments:

Post a Comment