How do I html_escape text data in a sinatra app?
Rack::Utils
includes a HTML escape method. http://www.sinatrarb.com/faq.html#escape_html
in sinatra using erubis, default setting escape_html is true. sometimes hava to unescape
Not sure about which version of Erubis you use, but it seems like it has a special kind of tag for that particular case: with two equals signs. So the line from your example might look like:
<%== "<h1>Thanks for help...</h1>" %>
Calling to CGI::unescape
should not be necessary, because the string is initially not escaped. All you need is to prevent escaping, not undo it.
But if your Erubis don't understand <%==
, or if you use ERB, not Erubis, then sorry, I don't know any other solution except of what you said: disable html escape for entire file and use h
everywhere you do need escaping.
FYI, in Rails for this also there are special helpers raw
and String#html_safe
, but as I can see they are part of ActiveSupport and not available in Sinatra.
Weird behavior of params hash in Sinatra
You seem to confuse the method params
for the local variable params
.
Let's take a look at a simplified example:
def params
{"action" => "some-action"}
end
# the line below refers to the method #params
p params # {"action" => "some-action"}
# the variable params is created, but not assigned
params = {n: 1} if false
# any further reference to params will use the variable
p params # nil
If you now have the question "why is the variable created?" the simple answer is: Because the documentation says so.
The local variable is created when the parser encounters the
assignment, not when the assignment occurs:a = 0 if false # does not assign to a
p local_variables # prints [:a]
p a # prints nil
To solve your issue either use the #params=
setter (assuming there is one) or retrieve the method result to a variable to start with, this way all references to params
refer to the variable.
# assign using the setter method
self.params = { :n => @number }
# extract the contents to a variable at the start
# of the `post '/modify' do` block
params = self.params # or params()
What is a better way to convert a simple sinatra app to static html pages?
Try Httrack
How could I escape a & in Haml so that it compiles to & instead of &? (Haml noob)
You can use the :escape_attrs
option to control whether HTML sensitive characters in attributes are escaped:
require 'haml'
haml = "%a(href='/posts' data-icon=\"&\" aria-hidden='true')"
puts Haml::Engine.new(haml, :escape_attrs => false).to_html
Output:
<a aria-hidden='true' data-icon='&' href='/posts'></a>
Note that this will apply to all attributes in your Haml template.
Displaying .txt file with Ruby Sinatra
You can do as below :
sinatra code :
get '/create' do
@logfile = File.open("logfile.txt","r")
erb :create
@logfile.close
end
file.erb
<h2>Text file:</h2>
<% @logfile.each_line do |line| %>
<%= line %>
<% end %>
Or you can use File#read
:
file.erb
<h2>Text file:</h2>
<%= @logfile.read %>
Creating a route with Sinatra to only accept a certain Content-type
Requests do not contain "Content-Type" header, but rather have "Accept". Sinatra should basically only respond to requests with "Accept" containing "application/json". Just to make sure:
post '/gods', :provides => :json do
pass unless request.accept? 'application/json'
...
end
Related Topics
How to Read a Clients Windows Login Name Using Ruby on Rails
How to Sort So That "Vitamin B12" Is Not in Front of "Vitamin B6"
Why Do Numeric String Comparisons Give Unexpected Results
"Bad Ecpoint" Ssl Error on Fresh Rvm Ruby 1.9.3 Install on Osx Mountain Lion
Open Xml File with Nokogiri Update Node and Save
Loop Within Loop in Rails Controller
Ruby - Convert Integer to String
Ruby Popen3 -- How to Repeatedly Write to Stdin & Read Stdout Without Re-Opening Process
How Does Sinatra Define and Invoke the Get Method
Why Does Single '=' Work in 'If' Statement
How to Efficiently Extract Repeated Elements in a Ruby Array
Ruby#Index Method VS Binary Search
Why Does Ruby '**' Operator Have Higher Precedence Than Unary '-'
Capture Webcam's Image with Ruby
How to Pass Value from One Resource to Another Resource in Chef Recipe
Ruby on Windows7 - Could Not Find Gem
How to Use Ruby Date Constants Gregorian, Julian, England and Even Italy