Undefined method html_safe for Nokogiri Html Document - ruby

I am trying to use .html_safe in the below description where I receive the error as Undefined method for Nokogiri HTML document.
blogs_controller.rb
#blog = Blog.find(19)
#description = Nokogiri::HTML.parse(#blog.description)
#description.search('a.fr-file').each do |desc|
desc['href']= File.join(ActionController::Base.asset_host, desc['href'])
end
show.html.erb
<p><%= #description.html_safe %></p>
Kindly advise.

html_safe is a Rails method defined on String but not on Nokogiri::HTML.
I would try to translate the Nokogiri document into a HTML first:
<p><%= #description.to_html.html_safe %></p>

Related

Hanami, show 'post' controller/view

I don't understand Hanami, I've created Message model, and I want to pull from DB message by params[:id]. The way we do it in Rails #message = Message.find(params[:id].
I read documentation, and what I did after. My Controller (for show):
def call(params)
#message = MessageRepository.find(params[:id])
end
And my erb:
<%= #message.title %>
But it gives me error:
NoMethodError: undefined method `title' for nil:NilClass
What I did wrong?
At the controller call expose :message, then you can use it in the view or in the template as local variable (without the #).

error undefined method in method call by pressing bootstrap button on index page Rails 4

I need to call method download_images("\folder","http:\url") which save pictures from url in choosen directory.This method should be called in index.html.erb and grab folder address from textbox1 and url from textbox2 after pressing the button1.
Right now I don't know how to grab strings from textboxes, I am trying to call method correctlyThe index.html.erb code:
<h1>Welcome#index</h1>
<p><%= "Download pictures from url!" %></p>
<div class="form-horizontal">
<p> Input url: </p>
<p> <input type="text"/> </p>
<p> Input destination folder: </p>
<p> <input type="text"/> </p>
<button class="btn">Go!</button>
<% button_to "btn", :method=> download_images("`/tutorial1/downloadedpics","http://www.yandex.ru/") %>
</div>
I defined method download_images in welcome_controller.rb:
class WelcomeController < ApplicationController
def index
end
def download_images(url, destination_path, options = {})
base_url = URI.join(url, "/").to_s
body = Typhoeus::Request.get(url).body
imgs = Nokogiri::HTML(body).css("img")
image_srcs = imgs.map { |img| img["src"] }.compact.uniq
hydra = Typhoeus::Hydra.new(:max_concurrency => options[:max_concurrency] || 50)
image_paths = image_srcs.map do |image_src|
image_url = URI.join(base_url, image_src).to_s
path = File.join(destination_path, File.basename(image_url))
request = Typhoeus::Request.new(image_url)
request.on_complete { |response| File.write(path, response.body) }
hydra.queue(request)
path
end
hydra.run
image_paths
end
end
After I switch server and go to localhost, I receive an exception:
NoMethodError in Welcome#index, undefined method download_images' for #<#<Class:0x007f202fc3ae50>:0x007f202f9ab518>, in line <% button_to "btn", :method=> download_images("/tutorial1/downloadedpics","http://www.yandex.ru/") %>
I am a noob programmer, so I can do rather dumb mistakes...
And it is important to mention: I work in Nitrous web box, and don't really know is it possible to download images in box folder:
~/tutorial1/downloadedpics
Also I use Bootstrap controllers,Nokogiri gem and Typhoeus gem.
Ruby version:ruby 2.1.1p76
Rails version:Rails 4.1.0
Thank you for your attention.
As a FYI, doing:
imgs = Nokogiri::HTML(body).css("img")
image_srcs = imgs.map { |img| img["src"] }.compact.uniq
is not the right way to find images with "src" parameters. Because you're not searching correctly, you get nils in your resulting array, forcing you to use compact. Instead, don't rely on cleaning up after making a mess, just avoid making the mess in the first place:
require 'nokogiri'
body = <<EOT
<html>
<body>
<img>
<img src="foo">
</body>
</html>
EOT
imgs = Nokogiri::HTML(body).css("img[#src]")
image_srcs = imgs.map { |img| img["src"] }
image_srcs # => ["foo"]

How can I access Middleman template helpers when extending a class in config.rb?

This config.rb works:
helpers do
def link_to_nothing(text)
link_to(text, "#")
end
end
With template index.html.erb:
<%= link_to_nothing "Test link" %>
But when I try to add a method to the Middleman::Sitemap::Resource class in this config.rb:
helpers do
class Middleman::Sitemap::Resource
def link(text)
link_to(text, path)
end
end
end
With template index.html.erb:
<%= current_page.link "This page" %>
The following error comes up when loading the page:
NoMethodError at /index.html
undefined method `link_to' for #<Middleman::Sitemap::Resource:0x3139848>
I've found that link_to is an instance method of class Middleman::Application, which I can access through the app variable:
helpers do
class Middleman::Sitemap::Resource
def link(text)
app.link_to(text, path)
end
end
end

Undefined method 'to_i' for :Symbol

RoR novice here, appreciate any help.
I have the following error:
undefined method `to_i' for :funding_level:Symbol
Am trying to pass both funding_level and investment_id parameters from link_to into the new method in my controller.
From my understanding, the 100 value gets passed as a Symbol and not an Object, causing problems when I then try to assign it to #funding_level, an integer variable.
Thanks!
HTML:
<div id="investment-status">
<%= link_to "$100", new_project_funding_path(funding_level: 100,investment_id: #project.id), class: 'btn' %>
</div>
Controller:
def new
#investment = Project.find(params[:investment_id])
#funding_offered = :funding_level
#project_funding = current_user.project_fundings.new(investment: #investment, funding_offered: #funding_offered)
end
Shouldn't be this instead ?
#funding_offered = params[:funding_level]

Passing variable from Sinatra module to view?

I have function in a module in the lib dir of my Sinatra app, that I want to be able to pass
a variable back to a view.
Trying to pass it like :
#errorMessage = params["testing error"]
erb :error
Bring the error erb that's in ../views from the lib directory, but does not show the errorMessage var.
erb code :
<p> Error message : <% #errorMessage %></p>
Anyone have any idea?
It should be <%= #errorMessage %> and not <% #errorMessage %>.
You can try using :locals
erb :error, :locals => {:errorMessage => "My message"}
And then use errorMessage as a variable inside your template.

Resources