Q:

Nokogiri returning href attribute as nil

I am building a small API with just two endpoints (at least by now) defined in my config/routes.rb by:
Rails.application.routes.draw do

  get '/api/list',      to: 'api#list'
  get '/api/add/:url',  to: 'api#add'

end

One of the endpoints receives an URL and I parse and store some of its contents with the following code in app/controllers/api_controller.rb:
require 'nokogiri'
require 'httparty'

class ApiController < ApplicationController

  def list
  end

  def add
    @url = "http://#{params[:url]}"
    site = Site.create(url: @url)
    site.save! 
    page = HTTParty.get(@url)
    doc = Nokogiri::HTML(page)
    ['h1','h2','h3','a'].each do |tag|
      doc.xpath("//#{tag}").each do |cont|
        if (tag == 'a') then
          content = Content.create({site_id: site.id, text: cont.at_xpath('/a/@href').to_s.strip!,  content_type: 'href', content_tag: tag })
        else 
          content = Content.create({site_id: site.id, text: cont.text().to_s.strip!,  content_type: 'tag_content', content_tag: tag })
        end
        content.save! 
      end
    end
  end

end

I managed to get the contents of <h1>, <h2> and <h3> tags perfectly, but the content of the href attributes for <a> tags is being saved as nil and I really don't know what is wrong with this:
cont.at_xpath('/a/@href').to_s.strip! 

expression. 
I already tried:
cont.at_xpath('/@href').to_s.strip! 

since cont is already the a node in this case, but it happened the same. 
I am using Rails 5 and I started my project with --api. 
Any hints? 

A:

Get the anchor tag first and then pass the href attribute. 
You get the anchor tag already with this code:
['h1','h2','h3','a'].each do |tag|
  doc.xpath("//#{tag}").each do |cont|
  end
end

Now you only use cont that contains the anchor tag to get its href using cont['href']:
content = Content.create({site_id: site.id, text: cont['href'], content_type: 'href', content_tag: tag })

