This is my first time creating a custom action so I’m a bit lost. I created a form where I enter a URL. This URL is passed to my controller where I use the URL to parse html (via Nokogiri).
The problem I am having is that I have no idea how to use this URL parameter that is being passed.
Here is the action in my controller:
def newdeal
url = @traveldeal.url #this is where I want to use the URL from my form_for
doc = Nokogiri::HTML(open(url))
title = doc.at_css("#content//h2/a").text
price = doc.at_css(".integer").text[/[0-9\.]+/]
link = doc.at_css("#content//h2/a")[:href]
desc = doc.at_css(".descriptor").text
Traveldeal.create(:title => title, :price => price, :url => link, :description => desc)
flash[:success] = "Deal successfully added!"
redirect_to root_path
end
The error I’m getting is undefined method 'url'. I’ve tried doing something like url = :url and even url = "#{:url}", but I get nil class error, so it appears that I’m just referencing it incorrectly.
The parameter is passed correctly though when I press submit:
{"utf8"=>"✓", "authenticity_token"=>"tGPYiuVO3dtRaNVvd6HAshqpTV7yXCmM1j0dXRSfpR4=", "traveldeal"=>{"url"=>"http://www.sampleurl.com"}, "button"=>""}
Here is the view:
<%= form_for @traveldeal, :url => newdeal_path, :method => 'post' do |f| %>
<div class="field">
<%= f.label :url %>
<%= f.text_field :url, :class => "round" %>
</div>
<div class="action">
<%= button_tag "", :class => "acctSubmit" %>
</div>
<% end %>
Thanks.
You need
@traveldeal = Traveldeal.new(params[:traveldeal])beforeurl = @traveldeal.urlin your action.