ruby - Rails + Nokogiri : controller create -
i creating simple_form allows users create new article in database providing link original article. if user provides url ("original_url"), use nokogiri fetch information.
i following error message: "no implicit conversion of nil string", tells me simple_form input field "original_url" not available used in controller / nokogiri.
is possible use variable simple_form before saving it?
my controller - create code:
def create if @original_url = nil @article = article.new(article_params) else @url = params[:original_url] #### think problem is. how pass "original_url" input controller? #### data = nokogiri::html(open(@url)) headline = data.at_css(".entry-title").text.strip @article = article.new(:headline => headline) end respond_to |format| if @article.save format.html { redirect_to @article, notice: 'article created.' } format.json { render :show, status: :created, location: @article } else format.html { render :new } format.json { render json: @article.errors, status: :unprocessable_entity } end end end
should call "original_url" variable in way?
first, use assignment in first condition:
if @original_url = nil
so @original_url
becomes nil
, condition never true. that's classic mistake.
that's how check nil
in ruby:
if @original_url.nil?
now, reason why @url
nil in params, have article
root. in case, need write:
@url = params[:acticle][:original_url]
Comments
Post a Comment