Rails: About partial templates

At first

** 24th day of calendar planning 2020 ** It's been about 3 months since I started studying programming, so I will leave a note of what I learned in the article as an output. I would be happy if I could help anyone entering the world of programming. Please let me know if there are any words that are wrong, wrong, or misunderstood ^^ I'm sorry if it's hard to read the words for a long time. I will do my best to get used to it little by little.

Rails: About partial templates (partial)

This is very convenient. I think it's good because I remember being well praised by the people who taught me that I was using partial templates. I don't know how good it is yet ^^; (The person who teaches me often said that it was partial, so I'll use it after pretending ^^) I can skip the HTML description, so I'm just using up where I can use it. If you don't know this easy way, please know it! Work speed will increase ^ ^

What are the benefits of partials? ??

From an amateur's point of view

  1. It's easy because there are only a few descriptions!
  2. It may be easier to see because the HTML description is reduced overall!
  3. It's easy because there are few parts to correct when correcting! And it's even better because there are no omissions!

1. It's easy because there are only a few descriptions

Since the place where the application is created and the same description is described is described using partials, only one line is required for the actual HTML. Actually write only once when creating a partial

Anyway, it is good to be able to reduce the description!

2. It may be easier to see because the HTML description is reduced overall.

Since HTML requires only one line, the amount is small overall and it is easy to see. It doesn't scroll down a lot, so it's helpful when you review it!

3. It's easy because there are few parts to correct when correcting! And it's even better because there are no omissions

This is the happiest place When correcting, if you correct only the original partial, the correction will be applied to all, so it will be easier to correct!

It was hell when the error occurred without knowing this. It took a long time to correct everything related but it was leaked, but if you use partial, it is easy because you can correct everything with one correction!

How to make a partial

This is also easy!

  1. Find a common description.
  2. When you find it, create a partial.
  3. Make a description to call the created partial in the corresponding place

the end!

ruby:show.html.erb



<% @posts.each do |post| %>
  <div class="post_<%= post.id %>">
    <%= post.member.name %>
    <%= post.title %>
    <%= post.body %>
  </div>
<% end %>

.
.
.
.


ruby:index.html.erb



<% @posts.each do |post| %>
  <div class="post_<%= post.id %>">
    <%= post.member.name %>
    <%= post.title %>
    <%= post.body %>
  </div>
<% end %>

Suppose you have a Post index and show like this. I want to display a list of Posts in both, so if I used the each statement, it would be a partial turn!

ruby:views/layouts/_post_index.html.erb



<% posts.each do |post| %>
  <div class="post_<%= post.id %>">
    <%= post.member.name %>
    <%= post.title %>
    <%= post.body %>
  </div>
<% end %>

Don't forget to start with (_) when creating a partial! The name is appropriate (I think a name that can be understood at a glance is good) After that, you can use it widely if you delete it as an instance variable (@). It works as a partial even if it is not erased, but it is not versatile.

ruby:show.html.erb



<%= render layouts/post_index, posts: @posts %>

ruby:index.html.erb



<%= render layouts/post_index, posts: @posts %>

Since I deleted (@) when creating the partial earlier, I added posts: @posts to specify it. For posts, specify the location you want to change to an instance variable, and describe the corresponding instance variable.

For example, it may be posts: @user depending on where you use it. It's a little difficult in this example, but ^^;

For flash messages and validation, it is convenient because you can use it just by creating a partial and rewriting it with the instant variable that matches each.

Finally

Partial wants to be more proficient! I recently learned that if you change the display location for each URL, you can make a partial, so I'm excited to see more partials.

Recommended Posts

Rails: About partial templates
About Rails 6
About Rails routing
[Rails] About ActiveJob ,!
About RSpec (Rails)
[Rails] About migration files
[Rails 6] About main gems
[Rails] About active hash
About rails application server
About rails kaminari pagination
About rails version specification
MEMO about Rails 6 series
[Rails] About Slim notation
[Rails] About collection when rendering partial (when loading partial template)
About rails strong parameters
[Beginner] About Rails Session
[Ruby on Rails] about has_secure_password
About naming Rails model methods
[Rails] About scss folder structure
Commentary on partial! --Ruby on Rails
[Rails] About Rspec response test
About Rails scraping method Mechanize
About partial match of selector
[Rails] About the Punk List function
About the symbol <%%> in Rails erb
Use partial templates (write maintainable code)
[Rails] About helper method form_with [Basic]
About =
Consideration about Rails and Clean Architecture
[Ruby on Rails] About bundler (for beginners)
[Rails 6.0] About batch saving of multiple records
[Ruby on Rails] About Active Record callbacks
[Rails] About local: true described in form_with
[rails] How to create a partial template
Rails: A little summary about data types