Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAustin Klenk
4,399 PointsUndefined 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
24,793 PointsYour 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
4,399 PointsAustin Klenk
4,399 PointsThats 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
24,793 PointsJustin Black
24,793 Pointshmm. What's the markup look like, and what's the User model look like?