[rails] How to create a partial template

What is a partial template?

A "partial template" is a View file that can be used in common on multiple pages. By combining the code that is duplicated in multiple files into one file, you only need to modify it in one place at the time of modification, and the code description is reduced and readability is improved.

The procedure is roughly like this.

  1. Find the parts that can be shared
  2. Create a partial template file
  3. Describe the common part in the partial template file
  4. Call the partial template file

1. Find the parts that can be shared

As it is, it is a task to find duplicates.

2. Create a partial template file

An erb file with an underscore (_) at the beginning of the file name is recognized as a partial template file. This time, we will look at the posting function as a partial template. app/views/books/_newform.html.erb

3. Describe the common part in the partial template file

Cut out the common part and paste it into the file. Basically, partial template files use local variables (those without @). This is because if you use an instance variable (the one with @) in the partial template file, when you change the name or behavior of the instance variable on the controller side, you also have to change the partial template side.

app/views/books/_newform.html.erb


<%= form_for(book) do |f| %>
	<div class="field row">
		<%= f.label :title %><br>
		<%= f.text_field :title, class: "col-xs-3 book_title" %>
	</div>
	<p></p>

	<div class="field row">
		<%= f.label :body %><br>
		<%= f.text_area :body, class: "col-xs-3 book_body" %>
	</div>

	<div class="actions row">
		<%= f.submit class: "btn btn-primary col-xs-3" %>
	</div>
<% end %>
</div>

4. Call the partial template file

The way to write when calling is as follows. When calling, omit the underscore of the partial template.

<%= render [Specifying a partial template file], [Local variables]:[Value to pass] %>

app/views/books/index.html.erb


<%= render 'books/newform', book: @book %>

Recommended Posts

[rails] How to create a partial template
[Rails] How to create a graph using lazy_high_charts
How to easily create a pull-down in Rails
[Rails] How to create a Twitter share button
How to create a method
[Rails] How to create a signed URL for CloudFront
Preparing to create a Rails application
[Java] How to create a folder
How to insert a video in Rails
How to create a Maven repository for 2020
[Swift5] How to create a splash screen
[Rails 6] How to create a dynamic form input screen using cocoon
How to write Rails
How to uninstall Rails
How to create docker-compose
Rails: How to write a rake task nicely
How to create a database for H2 Database anywhere
[Rails] How to write when making a subquery
[Rails] rails new to create a database with PostgreSQL
How to create pagination for a "kaminari" array
How to create a class that inherits class information
How to create a theme in Liferay 7 / DXP
[1st] How to create a Spring-MVC framework project
How to implement a like feature in Rails
Rails6.0 ~ How to create an eco-friendly development environment
How to make a follow function in Rails
How to create a query using variables in GraphQL [Using Ruby on Rails]
[Docker] How to create a virtual environment for Rails and Nuxt.js apps
[Rails] How to create a table, add a column, and change the column type
How to create an application
[rails] How to post images
[Introduction] Try to create a Ruby on Rails application
How to create a Java environment in just 3 seconds
[Rails] How to use enum
[Rails] How to install devise
[Rails] How to use enum
How to leave a comment
How to create a Spring Boot project in IntelliJ
How to read rails routes
[Spring Boot] How to create a project (for beginners)
How to create a data URI (base64) in Java
How to use rails join
[How to insert a video in haml with Rails]
How to write a date comparison search in Rails
How to terminate rails server
How to write Rails validation
How to create an Excel form using a template file with Spring MVC
How to write Rails seed
[Rails] How to use validation
[Rails 6] How to set a background image in Rails [CSS]
[Rails] How to disable turbolinks
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails] How to implement scraping
[Rails] How to make seed
How to write Rails routing
[Rails] How to load JavaScript in a specific view
[Rails] How to install simple_calendar
(Ruby on Rails6) How to create models and tables
[Rails] How to install reCAPTCHA
How to get started with creating a Rails app