As I was working on the task, I couldn't understand the "<% =%>" that came up, and I did some research. I found a collection of notes, so I'll leave it as a memorandum.
It seems that you can embed Ruby code in an HTML file by enclosing it in <%%> <% =%> in a Rails .erb format file. (Erb = Abbreviation for Embedded Ruby).
For example, if you enter the code below
ruby:top.html.erb
<% language = "Ruby" %>
<P><%= language %>learn</P>
↓ Result
localhost:3001
It will be. In addition, the definition of the variable of "<% language =" Ruby "%>" is generally defined by the action.
controller
def top
@language = Ruby
end
ruby:top.html.erb
<%= @language %>
ruby:top.html.erb
<%= form_for ('Model class instance') do |f| %>
Fill out the form
<% end %>
↓ If you change it to a code
ruby:top.html.erb
<%= form_for (@user) do |f| %>
<% f.text_field :name %>
<% f.submit %>
<% end %>
It's like that.
ruby:top.html.erb
<form class="top">
<input name="">
</form>
However, <% = form_for%> is recommended because it can be written shorter and security is improved.
Someone wrote about <%%> in more detail. Thank you very much. Meaning of symbols used in Rails erb
Recommended Posts