私はRailsが初めてです。in_group_of postsループでユーザーのアバターを取得するにはどうすればよいですか? Rails 4
私は投稿とユーザーです。ユーザーhas_many
投稿と投稿belongs_to
ユーザー。
次のコードを実行すると、同じユーザーのアバターを3回取得します。
ただし、各ユーザーには独自のアバターがあります。 各ユーザーの正しいユーザーのアバターをdivカードに登録するにはどうすればよいですか?
<% @Posts.limit(3).in_groups_of(3, false).each do |group| %>
<div class="row">
<% group.each do |post| %>
<div class="col-xs-12 col-sm-4">
<% @user.limit(1).each do |user| %>
<div class="card">
<img src="<%= user.image.url(:thumb) %>" alt="<%= user.name %>" class="avatar-small card-user">
<% end %>
<div class="card-description">
<a href="<%= posts_path(post.slug) %>"><h3><%= post.title %></h3></a>
<p><%= post.subtitle %></p>
</div>
</div></a>
</div>
<% end %>
</div>
<% end %>
私のコントローラ:
class PagesController < ApplicationController
def index
@Posts = Post.all
@user = User.all
end
end
schema.rb:私はペーパークリップを使用して宝石を管理
create_table "users", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
end
create_table "posts", force: :cascade do |t|
t.string "title"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
t.integer "user_id"
t.string "slug"
t.string "subtitle"
end
。
ありがとうございました。 もっと情報が必要な場合は教えてください。
を.includes(:users).limit(3)そしてすべての投稿には独自のユーザーがあります。 – Milind