LaFrish LaFrish - 4 months ago 12
Ruby Question

Rails: Trying to render image of users with same attribute

I am making a yearbook application in Rails. I am currently trying to display a list of images of all the Wdiers (this is a combo of teachers and students) that have the same

squad_id
as the wdier's profile page (aka the
show
view). This would be rendered under the
squad_name
of the Wdier on the
show
page.

Here is the
show
view:

<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>


I keep getting this error message:


undefined method `each' for nil:NilClass


Here is my controller for 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


Here is my schema for 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


What will it take to solve the current error and display the list of Wdiers in the same squad?

Answer

In your show action in the controller, you need to perform a search for the squad members:

  def show
    @wdier = Wdier.find(params[:id])
    @wdiers = Wdier.where(squad_id: @wdier.squad_id).not(id: @wdier.id).order(:name)
  end

This will retrieve into @wdiers all of the Wdiers that have the same squad_id. The Wdier on the show page is left out of the squad list, because that Wdier is already displayed on the show page. The list of Wdiers is sorted by name, just like a yearbook.