我在Rails上做年鉴申请我目前正试图显示所有WDIER(这是教师和学生的组合)的图像列表,这些图像与WDIER的配置文件页(也称为squad_id
视图)具有相同的show
。这将在squad_name
页上的Wdier的show
下呈现。
以下是show
视图:
<div class="squadGallery">
<ul class="grid">
<div class="index">
<h2> <%= @wdier.squad_name %></h2>
<% @wdiers.each do |wdier| %>
<%= link_to image_tag(wdier.img_url), wdier_path(wdier) %>
<% end %>
</ul>
</div>
</div>
我一直收到这个错误消息:
nil的未定义方法“each”:nilclass
这是我的wdiers控制器:
class WdiersController < ApplicationController
def index
@wdiers = Wdier.all
end
def new
@wdier = Wdier.new
end
def create
@wdier = Wdier.create!(wdier_params)
redirect_to wdiers_path(@wdier)
end
def show
@wdier = Wdier.find(params[:id])
end
def edit
@wdier = Wdier.find(params[:id])
end
def update
@wdier = Wdier.find(params[:id])
@wdier.update(wdier_params)
redirect_to wdier_path(@wdier)
end
def destroy
@wdier = Wdier.find(params[:id])
@wdier.destroy
redirect_to wdiers_path(@wdier)
end
def wdier_params
params.require(:wdier).permit(:name, :img_url, :squad_name, :squad_id, :quote, :teaching, :memory, :favlang, :wisewords, :tag_list,:github_url, :portfolio_url, :project1_url, :project2_url, :project3_url, :quote, :q1, :q2, :q3, :fb, :linkedin, :email, :role, :student_id, :instructor_id)
end
end
这是我的wdiers模式:
create_table "wdiers", force: :cascade do |t|
t.string "name"
t.string "img_url"
t.string "github_url"
t.string "portfolio_url"
t.string "project1_url"
t.string "project2_url"
t.string "project3_url"
t.string "quote"
t.string "squad_name"
t.integer "teaching"
t.integer "memory"
t.integer "favlang"
t.integer "wisewords"
t.integer "tag_list"
t.integer "q1"
t.integer "q2"
t.integer "q3"
t.integer "fb"
t.integer "linkedin"
t.integer "email"
t.integer "role"
t.integer "password"
t.integer "squad_id"
t.integer "codey_id"
t.integer "student_id"
t.integer "instructor_id"
end
add_index "wdiers", ["codey_id"], name: "index_wdiers_on_codey_id", using: :btree
add_index "wdiers", ["instructor_id"], name: "index_wdiers_on_instructor_id", using: :btree
add_index "wdiers", ["squad_id"], name: "index_wdiers_on_squad_id", using: :btree
add_index "wdiers", ["student_id"], name: "index_wdiers_on_student_id", using: :btree
add_foreign_key "comments", "wdiers"
end
要解决当前的错误并显示同一个队的球员名单需要什么?
最佳答案
在控制器中的show
操作中,需要搜索班子成员:
def show
@wdier = Wdier.find(params[:id])
@wdiers = Wdier.where(squad_id: @wdier.squad_id).not(id: @wdier.id).order(:name)
end
这将检索到所有具有相同
@wdiers
的单词squad_id
页面上的Wdier不在队伍列表中,因为该Wdier已经显示在show
页面上单词列表按show
排序,就像年鉴一样。