About the symbol <%%> in Rails erb

What is <%%> / <% =%>

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

スクリーンショット 2020-11-05 23.20.13.png

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 %>

How to use <% = form_for%>

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.

Reference article

Someone wrote about <%%> in more detail. Thank you very much. Meaning of symbols used in Rails erb

Recommended Posts

About the symbol <%%> in Rails erb
about the where method (rails)
Questions about implementing the Like feature (Ajax) in Rails
[Rails] About the Punk List function
About Rails 6
The identity of params [: id] in rails
Rails refactoring story learned in the field
[Rails] About local: true described in form_with
[Rails] Reset the database in the production environment
Rails6: Extract the image in Action Text
About Rails routing
[Rails] About the error that the image is not displayed in the production environment
About the method
Group_by in Rails
[Rails] Use devise to get information about the currently logged in user
[Rails] About ActiveJob ,!
About the package
About Rails controller
About RSpec (Rails)
[Order method] Set the order of data in Rails
About the confusion seen in startup Java servers
[Rails / ActiveRecord] About the difference between create and create!
I tried to organize the session in Rails
rails tutorial About account activation in production environment
[Rails] Show multi-level categories in the breadcrumb trail
Implement the Like feature in Ajax with Rails.
A note about the Rails and Vue process
How to check Rails commands in the terminal
rails code doesn't respond well in atom erb
About the case where "Docker" freeter tried to put Docker in the existing Rails application
Output about the method # 2
[Rails] I learned about the difference between resources and resources
[Rails] About migration files
About the StringBuilder class
How to set the display time to Japan time in Rails
[Rails 6] About main gems
Commentary: About the interface
[Rails] About active hash
[Ruby on Rails Tutorial] Error in the test in Chapter 3
About rails application server
Model association in Rails
About the asset pipeline
About the function double-java
About eval in Ruby
About rails kaminari pagination
About rails version specification
About the problem of deadlock in parallel processing in gem'sprockets' 4.0
About the difference between classes and instances in Ruby
Disable turbolinks in Rails
About the ternary operator
CSRF measures in Rails
MEMO about Rails 6 series
[Ruby / Rails] Set a unique (unique) value in the class
About the length method
About the Kernel module
[Rails] About Slim notation
^, $ in Rails regular expression
Use images in Rails
About the authenticate method.
Understand migration in rails
About the map method