我有一個模型與其他 3 個模型有多對多的關系
楷模
class User < ApplicationRecord
# == Relationships ==============================================================================
has_and_belongs_to_many :hobbies, join_table: 'hobbies_users'
has_and_belongs_to_many :jobs, join_table: 'jobs_users'
has_and_belongs_to_many :degrees, join_table: 'degrees_users'
end
class Degree < ApplicationRecord
has_and_belongs_to_many :users, join_table: 'degrees_users'
end
其他型號看起來一樣。
我也有一個用戶控制器和他的 index 方法
控制器
class UsersController < ApplicationController
def index
@users = User.all
end
end
看法
在視圖中,我想在卡片“共享”布局中呈現每個用戶。
<h1>Usuarios</h1>
<div >
<%= @users.each do |user| %>
<%= render "card", user: user %>
<% end %>
</div>
共享卡
這是一張來自模板的卡片。
<div class="frontside">
<div class="card">
<div class="card-body text-center">
<p><%= image_tag "https://images.pexels.com/photos/10474224/pexels-photo-10474224.jpeg?cs=srgb&dl=pexels-rodnae-productions-10474224.jpg&fm=jpg", class: "img-fluid" %></p>
<h4 > <%= user.name %></h4>
<p class="card-text"><%= user.description %></p>
<a href="https://www.fiverr.com/share/qb8D02" ><i ></i></a>
</div>
</div>
</div>
在本地瀏覽器上
當我查看本地主機中的用戶索引時,它會為每個用戶呈現 de 卡片,但最后它將底部的 ActiveRecord 陣列呈現為純文本。
我不明白為什么。
這是我所說的螢屏截圖。我不知道如何擺脫它
螢屏截圖
uj5u.com熱心網友回復:
只需=從迭代所有用戶的行中洗掉并將其更改為:
<% @users.each do |user| %>
<%= render "card", user: user %>
<% end %>
ERB 中的A<%=表示您要輸出以下陳述句的結果。這就是您想要的,<%= render "card", user: user %>因為這些行回傳呈現的 HTML。
但<% @users.each do |user| %>僅用于迭代所有用戶,您不想輸出@users.each呼叫的回應,它只是一個字串表示Enumerator
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/510059.html
