[RUBY] [Rails] views file creation # text is displayed

# Text is displayed

The string #text that was not displayed was displayed, so I investigated it.

スクリーンショット 2020-12-30 23.17.14.png

Wrong source code index.html.erb ↓

<h1>top page</h1>
<%= @posts.each do |post| %>
  <div>
    <%= post.memo %>
    <%= post.created_at %>
  </div>
<% end %>

posts_controller.rb ↓

class PostsController < ApplicationController
  def index
    @posts = Post.all
  end
end

Bring the information in the DB with @posts = Post.all, pass it to html, and embed it in html with <%%>. Looking at the html file, the second line is <% =%>, so it was said that @posts was also displayed. Only post.memo and post.created_at are displayed this time, so the correct answer is ↓

<h1>top page</h1>
<% @posts.each do |post| %>
  <div>
    <%= post.memo %>
    <%= post.created_at %>
  </div>
<% end %>

Summary

Display by embedding in html with <% =%>. Just embed it in html with <%%>.

Recommended Posts

[Rails] views file creation # text is displayed
[Rails] fields_for is not displayed
Save/load text file
java file creation
Rails Action Text implementation
Rails is Owakon www