I want to extract the search form, from this webpage, and render it on the "static_pages/home" page of my Rails app: Codepen Example of "static_pages/home"
Steps taken:
I created the following Ruby script to verify that I could actually extract the form:
require 'nokogiri' require 'open-uri' url = 'http://websoc.reg.uci.edu/perl/WebSoc' data = Nokogiri::HTML(open(url)) form = data.xpath('//form[@action="http://websoc.reg.uci.edu/perl/WebSoc"]') puts form
Shifting over to Rails, I included Nokogiri and OpenURI in my gem file and used bundle to install the gems.
I created a StaticPages controller:
class StaticPagesController < ApplicationController def home require 'nokogiri' require 'open-uri' url = 'http://websoc.reg.uci.edu/perl/WebSoc' data = Nokogiri::HTML(open(url)) @form = data.xpath('//form[@action="http://websoc.reg.uci.edu/perl/WebSoc"]') end end
And an accompanying view:
<h1>StaticPages#home</h1> <p>Find me in app/views/static_pages/home.html.erb</p> <%= @form %>
The HTML code is successfully extracted but it is rendered as text instead of HTML. It seems like either:
@form = data.xpath('//form[@action="http://websoc.reg.uci.edu/perl/WebSoc"]')
or
<%= @form %>
converts the extracted HTML to text. How can I insert the HTML content I have extracted as HTML and not as text?
My research has suggested using Net:HTTP.