Weblio's url format is like this: http://ejje.weblio.jp/{desired
english word}
Well, not quite: you can't have spaces in a url, so you have to do something extra to a phrase like "a lot of"
.
In rails, by default you use something called ERB to substitute values into a view. ERB allows you to write ruby code in your html and execute it. All your view files have the extension .html.erb
--that is a hint that rails first runs your views through the ERB templating engine to produce the .html files. So, you need to read a tutorial on ERB syntax, such as this one. Here is an example of what ERB looks like:
<%
replacements = {
'1杯の'=>'a cup of',
'たくさん'=>'a lot',
'たくさんの'=>'a lot of',
}
replacements.values.each do |phrase| %>
<a href="http://ejje.weblio.jp/<%= URI.escape(phrase) %>"><%= phrase %></a>
<% end %>
A <%= .... %>
block inserts the results of the ruby code contained in the block into the html.
A <% ..... %>
block is for ruby code that you want to be executed but whose results are not to be inserted into the html.
If you create your replacements in an action, e.g.:
def dostuff
@replacements = {
...
}
end
then your view would simply look like this:
<% @replacements.values.each do |phrase| %>
<a href="http://ejje.weblio.jp/<%= URI.escape(phrase) %>"><%= phrase %></a>
<% end %>
Below is an example that demonstrates the use of the ERB templating engine outside of rails.
require 'erb'
require 'uri'
template = %q{ #=> Another way in ruby to write an opening single quote for a string.
<%
replacements = {
'1杯の'=>'a cup of',
'たくさん'=>'a lot',
'たくさんの'=>'a lot of',
}
replacements.values.each do |phrase| %>
<a href="http://ejje.weblio.jp/<%= URI.escape(phrase) %>"><%= phrase %></a>
<% end %>
} #=>Closing single quote for the string
renderer = ERB.new(template)
result = renderer.result()
puts result
--output:--
<a href="http://ejje.weblio.jp/a cup of">a cup of</a>
<a href="http://ejje.weblio.jp/a lot">a lot</a>
<a href="http://ejje.weblio.jp/a lot of">a lot of</a>
In rails, you don't need to write the require
or ERB.new()
or renderer.result()
--rails does all that automatically for an .html.erb
file
Once you write a few views using ERB, and you feel comfortable with the basics, you can promptly forget about ERB and use one of the better templating languages, like slim, which is much prettier and easier to type than ERB. Rails has the means for incorporating any templating engine of your choice--but the default is ERB.
Response to comment:
def dostuff
replacements = {
'1杯の'=>'a cup of',
'たくさん'=>'a lot',
'たくさんの'=>'a lot of',
}
replacements.each do |key, val|
replacements[key] = %Q{<a href="http://ejje.weblio.jp/#{URI.escape(val)}">#{val}</a>}
end
require 'pp' #pretty print
pp replacements
end
Output in server window:
{"1杯の"=>"<a href=\"http://ejje.weblio.jp/a%20cup%20of\">a cup of</a>",
"たくさん"=>"<a href=\"http://ejje.weblio.jp/a%20lot\">a lot</a>",
"たくさんの"=>"<a href=\"http://ejje.weblio.jp/a%20lot%20of\">a lot of</a>"}
Response to comment:
Your str is html escaped. If you need to literally display all the characters in the string <a>hello</a>
on a web page, you must replace the angle brackets with what are called html entities, otherwise the browser will render the <a>
tag as a link. The html entity for the <
character is <
, where lt
stands for less than, as in a less than symbol in a mathematical comparison: if x < 4
. Similarly, the html entity for the >
character is >
, as in greater than.
require 'cgi'
str = %q{<a href="http://ejje.weblio.jp/content/Windows">Windows</a>}
puts "<div>This is an html instruction site. First we will start with link tags.</div>"
puts "<div>Link tags look like this:</div>"
puts "<div>#{CGI.escapeHTML str}</div>"
str = %q{<a href="http://ejje.weblio.jp/content/Windows">Windows</a>}
html = CGI.unescapeHTML str
puts "<div>Here is what that link looks like when it is rendered by a browser:</div>"
puts "<div>#{html}</div>"
--output:--
<div>This is an html instruction site. First we will start with link tags.</div>
<div>Link tags look like this:</div>
<div><a href="http://ejje.weblio.jp/content/Windows">Windows</a></div>
<div>Here is what that link looks like when it is rendered by a browser:</div>
<div><a href="http://ejje.weblio.jp/content/Windows">Windows</a></div>
If you take the output of that program and open it in a browser, you will see:
This is an html instruction site. First we will start with link tags.
Link tags look like this:
<a href="http://ejje.weblio.jp/content/Windows">Windows</a>
Here is what that link looks like when it is rendered by a browser:
Windows
tag and not linking. How I can escape tag form
– LEoREo_2247 Jun 13 '16 at 16:22?