** 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.
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 ^ ^
From an amateur's point of view
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!
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!
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!
This is also easy!
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.
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