[RUBY] Resolved strange HTML display by reviewing Rails dynamic processing

Just change <% =%> to <%%> ...

[Mentor who gave me advice]

【Operating environment】 macOS 10.15.6 Rails 6.0.3.3 Ruby 2.6.6


This is a story when I was working on a school assignment with Ruby on Rails.

I implemented a database search function in my Rails app.

Create a User model and migrate the database with the columns as "name" and "age".

The initial data was input using "faker", and the list display page was created.

index.html.erb


<%= search_form_for @q do |f| %>
  <%= f.label :name_cont, "Full name" %>
  <%= f.search_field :name_cont %>
  <%= f.label :age_eq, "age" %>
  <%= f.search_field :age_eq %>
  <%= f.submit "Search" %>
<% end %>

<table>
  <tbody>
    <thead>
      <tr>
        <td>name</td>
        <td>age</td>
      </tr>
    </thead>
    <%= @users.each do |user| %>
    <tr>
      <td><%= user.name %></td>
      <td><%= user.age %></td>
    </tr>
    <% end %>
  </tbody>
</table>

<%= paginate @users %>

And when I started the Rails server,

1.png

Oh, the database is displayed! ??

There is no error, only the screen display is strange ... I don't know the mistake because there is no error statement ... I can't tell the mistake even if I look back at the teaching materials ...

I was worried for two days. If you talk to your mentor sooner ...

The solution is a little below the center of the code above,

    <%= @users.each do |user| %>

To

    <% @users.each do |user| %>

Just changed to.

Do you understand?

I just changed the <% = at the beginning of the code statement to <%.

With just this

スクリーンショット 2020-10-05 10.10.08.png

It was displayed neatly.

It seems that we need to get a better understanding of Rails' dynamic processing.


Thank you for reading until the end.

If you like, I also do Twitter, so if you are interested, please follow me.


Recommended Posts

Resolved strange HTML display by reviewing Rails dynamic processing
[Rails] Regular batch processing by whenever