Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What is going on with this article?
@Masashi9410

eachで回す時の<% %>と<%= %>の違い

More than 1 year has passed since last update.

eachで回していて、
なぜか、以下の画像のように出力されました。
スクリーンショット 2019-10-20 21.12.05.png

[#<User id: 1, name: "aaaa", email: "aaaaa", created_at: "2019-10-19 12:10:03", updated_at: "2019-10-19 12:10:03">,
 #<User id: 2, name: "bbb", email: "bbbb", created_at: "2019-10-19 12:10:35", updated_at: "2019-10-19 12:10:35">]

この部分いらないな。てか、なんで出てきたんだろう。今までこんな事なかったのに、、、
と思っていたところ、問題は、viewでした。

index.html.erb
<h1>Users#index</h1>
<p>Find me in app/views/users/index.html.erb</p
<div>
  <%= @user.each do |user| %>
    <p>
      <%= user.name %>
      <%= user.email %>
    </p>
  <% end %>
</div>

<%= @user.each do |user| %>の=が問題でした。
<% @user.each do |user| %>とすると、いらない部分は消えました。
=をつけると、データ側も出力されるという新しい発見でした笑

0
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Masashi9410
Webエンジニア Java/Ruby/Rails/HTML/CSS/js

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
0
Help us understand the problem. What is going on with this article?