tommyd456
tommyd456

Reputation: 10673

Nokogiri and Rails - Small Issue

I'm using Nokogiri to parse this XML:

<?xml version="1.0" encoding="utf-8"?>
<GetPropertiesResponse>
 <Properties>
  <Property>
   <Id>19</Id>
   <Name>Property 1</Name>
   <Breakfast></Breakfast>
   <Currency>GBP</Currency>
  </Property>

  <Property>
   <Id>13</Id>
   <Name>Property 2</Name>
   <Breakfast>IN</Breakfast>
   <Currency>EUR</Currency>
  </Property>

  <Property>
   <Id>15</Id>
   <Name>Property 3</Name>
   <Breakfast>EX</Breakfast>
   <Currency>GBP</Currency>
  </Property>

 </Properties>
</GetPropertiesResponse>

like this:

...
doc = Nokogiri::XML(response.body)
@property = doc.xpath("//Property")

and then in my view I'm displaying each property name like so:

<%= @property.each do |item| %>
  <%= item.xpath("Name").text %><br>
<% end %>

but my output always has a mysterious 0 on the end and I'm not sure why? My output looks like this:

Property 1
Property 2
Property 3
0

Upvotes: 0

Views: 44

Answers (1)

engineersmnky
engineersmnky

Reputation: 29318

This

<%= @property.each do |item| %>
  <%= item.xpath("Name").text %><br>
<% end %>

Has an extra = sign

It should be:

<% @property.each do |item| %>
  <%= item.xpath("Name").text %><br>
<% end %>

The 0 is because when @property.each completes it returns 0 if you remove the = the return value is not shown

Upvotes: 2

Related Questions