store html symbol in variable - html

I need to print stored html symbols in an email. Why isn't this working? It prints the symbol string instead of the symbol. ex: ↺ instead of ↺
here is a example of my code:
<% event_symbol = "↺"%>
<%="#{event_symbol}"%>

Add raw:
<% event_symbol = "↺"%>
<%=raw event_symbol%>

Related

undefined method `strftime' for "{1=>2019, 2=>7, 3=>7, 4=>16, 5=>0}":String Did you mean? strip

I am trying to have the time output in a nice way.
Here is my code in new
<%= form_for #reservation do |f| %>
<span><label>Pick up time</label></span>
<%= f.time_select :time,ampm: true, class: "textbox"%></span>
<% end %>
And here is in the output file
<strong>Pick up Time:</strong> <%= #reservation.time.strftime("%H:%M") %>
I got this error
undefined method `strftime' for "{1=>2019, 2=>7, 3=>7, 4=>16, 5=>0}":String
Did you mean? strip
Please some one help me to fix this
As Sebastian mentions in a comment; it looks as though your time object is a Hash. To call strftime your object needs to be a :datetime.
To fix this you should change the column type in your database to be that of datetime (As well as changing your forms to save a datetime value).
An alternative would be to build the string you want to display manually using what is in time (not using strftime).
If you want to use only this hash then you need to parse it to date time like below
a = {1=>2019, 2=>7, 3=>7, 4=>16, 5=>0}
time = (a.values.first(3).join("/") + " "+ a.values.last(2).join(":")).to_datetime.strftime("%I:%M %p")
After parsing to date and time you can perform any method of date time.
(#reservation.to_date).strftime("%H:%M")
It seems to be an order of operations - first change string to date then transform. Something similar worked for me in Workato just now.

Apex - Generate HTML form through string

Can I create a HTML form through an apex String ? For instance, I want something like this.
String example = "<p> This is my example </p> <br/>";
But what it returns is " This is my example ", HTML tags does not have its real meaning in it. Is there any other way to achieve this ?
Thank You
What if you replace the html elements with their html entity counterparts:
String example = "<p> This is my example </p&gt <br/&gt";

#{} is not working on ruby on rails [duplicate]

This question already has an answer here:
String interpolation in Ruby doesn't work?
(1 answer)
Closed 5 years ago.
I've been trying to show random images everytime a user post something.
I have images name "kejang (#).png" # ranging from 1 to 21.
So I have done this.
<div class = 'article_header'>
<div class = 'article_img'>
<% kj_num = rand(20) + 1 %>
<%= kj_num %>
<%= image_tag 'kejang (#{kj_num}).PNG' %>
</div>
It does run on the server but returns no image, only 'x' marked crashed images.
I wondered where it went wrong and altered #{kj_num} with regular number and it worked. (i.e, I put 2 or 3 instead of #{kj_num} and it worked.)
Why is #{kj_num} not working ??
Rails interpretting #{variable} as a simple text. How should I change my code?
#{} vs. single vs. double quotes
Here you need to use double quotes (") rather than single quotes (')
<div class = 'article_header'>
<div class = 'article_img'>
<% kj_num = rand(20) + 1 %>
<%= kj_num %>
<%= image_tag "kejang (#{kj_num}).PNG" %>
</div>
</div>
String interpolation is the process of converting placeholders inside a string to the values they represent, in order to produce a dynamic string. You probably use it all the time without realizing just how cool it works under the hood, and how you can leverage this power in your own classes.
person = 'World'
puts "Hello, #{person}!"
https://kconrails.com/2010/12/08/ruby-string-interpolation/
You are trying to use string interpolation inside single quotes which will not work. You have to use double quotes instead
Here is an example to understand it,
jruby-1.7.13 :001 > name = 'ajinkya'
=> "ajinkya"
jruby-1.7.13 :002 > 'hello, #{name}'
=> "hello, \#{name}"
jruby-1.7.13 :003 > "hello, #{name}"
=> "hello, ajinkya"
More detailed explanation about Double vs single quotes

Keep Special Characters when parsing JSON response

I have retrieve this key/value from a hash using the facebook api
"message":"Next Practice:\n\nDate: 04.05.2014\nTime: 10:00-12:00\nVenue: Llandaff Fields\n\nAll welcome
but when i save it to my model i seem to lose all the special characters, i.e \n. Is there a way to save the value as it is returned so that i can use the \n when outputting to my view using .html_safe
This is how i am retrieving the data
def get_feed
fb_access_token = access_token
uri = URI(URI.escape "https://graph.facebook.com/#{VANDALS_ID}/posts/?#{fb_access_token}")
response = HTTParty.get(uri)
results = JSON.parse(response.body)
formatted_data(results)
end
anything i need to be doing to keep that string with \n left in it
Thanks
When I run the following code:
raw_json = '{"message":"Next Practice:\n\nDate: 04.05.2014\nTime: 10:00-12:00\nVenue: Llandaff Fields\n\nAll welcome"}'
parsed_json = JSON.parse(raw_json)
puts parsed_json['message']
# => Next Practice:
# => Date: 04.05.2014
# => Time: 10:00-12:00
# => Venue: Llandaff Fields
# => All welcome
So the \n is kept (it is parsed, and shown as real new-line). I also don't believe that saving this to your model erased the new lines.
Where I think your real problem lies is that in HTML new lines (\n) are not rendered as new lines at all, but as spaces. To render them as new lines, you need to replace them with breaks (<br>).
So you can try using the following on your ERB:
<div class=message><%= feed.message.gsub("\n", "<br>").html_safe %></div>
Your new-lines will now be rendered on the page.

How to HTML encode/escape a string? Is there a built-in?

I have an untrusted string that I want to show as text in an HTML page. I need to escape the chars '<' and '&' as HTML entities. The less fuss the better.
I'm using UTF8 and don't need other entities for accented letters.
Is there a built-in function in Ruby or Rails, or should I roll my own?
Checkout the Ruby CGI class. There are methods to encode and decode HTML as well as URLs.
CGI::escapeHTML('Usage: foo "bar" <baz>')
# => "Usage: foo "bar" <baz>"
The h helper method:
<%=h "<p> will be preserved" %>
In Ruby on Rails 3 HTML will be escaped by default.
For non-escaped strings use:
<%= raw "<p>hello world!</p>" %>
ERB::Util.html_escape can be used anywhere. It is available without using require in Rails.
An addition to Christopher Bradford's answer to use the HTML escaping anywhere,
since most people don't use CGI nowadays, you can also use Rack:
require 'rack/utils'
Rack::Utils.escape_html('Usage: foo "bar" <baz>')
You can use either h() or html_escape(), but most people use h() by convention. h() is short for html_escape() in rails.
In your controller:
#stuff = "<b>Hello World!</b>"
In your view:
<%=h #stuff %>
If you view the HTML source: you will see the output without actually bolding the data. I.e. it is encoded as <b>Hello World!</b>.
It will appear an be displayed as <b>Hello World!</b>
Comparaison of the different methods:
> CGI::escapeHTML("quote ' double quotes \"")
=> "quote ' double quotes ""
> Rack::Utils.escape_html("quote ' double quotes \"")
=> "quote ' double quotes ""
> ERB::Util.html_escape("quote ' double quotes \"")
=> "quote ' double quotes ""
I wrote my own to be compatible with Rails ActiveMailer escaping:
def escape_html(str)
CGI.escapeHTML(str).gsub("'", "'")
end
h() is also useful for escaping quotes.
For example, I have a view that generates a link using a text field result[r].thtitle. The text could include single quotes. If I didn't escape result[r].thtitle in the confirm method, the Javascript would break:
<%= link_to_remote "#{result[r].thtitle}", :url=>{ :controller=>:resource,
:action =>:delete_resourced,
:id => result[r].id,
:th => thread,
:html =>{:title=> "<= Remove"},
:confirm => h("#{result[r].thtitle} will be removed"),
:method => :delete %>
<a href="#" onclick="if (confirm('docs: add column &apos;dummy&apos; will be removed')) { new Ajax.Request('/resource/delete_resourced/837?owner=386&th=511', {asynchronous:true, evalScripts:true, method:'delete', parameters:'authenticity_token=' + encodeURIComponent('ou812')}); }; return false;" title="<= Remove">docs: add column 'dummy'</a>
Note: the :html title declaration is magically escaped by Rails.