Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby

Austin Klenk
Austin Klenk
4,399 Points

Undefined Method 'user'

Im getting a undefined method of user when i call for

  <%= @department.user.firstName %>

Department Controller..

class DepartmentsController < ApplicationController
  before_action :set_department, only: [:show, :edit, :update, :destroy]

  # GET /departments
  # GET /departments.json
  def index
    @departments = Department.all
  end

  # GET /departments/1
  # GET /departments/1.json
  def show
     @department = Department.find(params[:id])
  end

  # GET /departments/new
  def new
    @department = Department.new
  end

  # GET /departments/1/edit
  def edit
  end

  # POST /departments
  # POST /departments.json
  def create
    @department = Department.new(department_params)

    respond_to do |format|
      if @department.save
        format.html { redirect_to @department, notice: 'Department was successfully Created.' }
        format.json { render :show, status: :created, location: @department }
      else
        format.html { render :new }
        format.json { render json: @department.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /departments/1
  # PATCH/PUT /departments/1.json
  def update
    respond_to do |format|
      if @department.update(department_params)
        format.html { redirect_to @department, notice: 'Department was successfully Updated.' }
        format.json { render :show, status: :ok, location: @department }
      else
        format.html { render :edit }
        format.json { render json: @department.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /departments/1
  # DELETE /departments/1.json
  def destroy
    @department.destroy
    respond_to do |format|
      format.html { redirect_to departments_url, notice: 'Department was successfully Deleted.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_department
      @department = Department.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def department_params
      params.require(:department).permit(:name, :user_id)
    end
end

When I checked my migrations and there is a firstName field in the migration and here is the department model

class Department < ActiveRecord::Base
    has_many :users
end

also in another file file i am asking for a table calls with in a log index and i am getting a

undefined method `each' for nil:NilClass

1 Answer

Justin Black
Justin Black
24,266 Points

Your instance variable is @departments

So in your view when you are doing something like:

 <% @deparments.each do |department| %>
      ... code here ...
    <% end %>

then department is a singular iteration of the instance variable creating a local variable to that view. So, your implementation would be more so like this:

<%= department.user.firstName %>

note: this only holds true if we're talking about the index.

Austin Klenk
Austin Klenk
4,399 Points

Thats what I have is the @departments in the index view, but when i go to add another table header with Say Employee, then i pass in

<%= department.user.firstName %>

i still get a undefined method of user :(

Justin Black
Justin Black
24,266 Points

hmm. What's the markup look like, and what's the User model look like?