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 trial

Ruby

alborz
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
alborz
Full Stack JavaScript Techdegree Graduate 30,885 Points

Getting undefined method `email' for nil:NilClass error when rendering post.user.email in index.html.haml view

I'm getting an undefined method `email' for nil:NilClass when rendering post.user.email in my index.html.haml view. I was able to render post.user successfully, but began to get the error when refreshing the application after adding post.user.email.

Posts controller:

class PostsController < ApplicationController
  before_action :find_post, only: [:show, :edit, :update, :destroy]

  def index
    @posts = Post.all.order("created_at DESC")
  end

  def show
  end

  def new
    @post = current_user.posts.build
  end

  def create
    @post = current_user.posts.build(post_params)

    if @post.save
      redirect_to @post
    else
      render 'new'
    end
  end

  def edit
  end

  def update
    if @post.update(post_params)
      redirect_to @post
    else
      render 'edit'
    end
  end

  def destroy
    @post.destroy
    redirect_to root_path
  end

  private

  def find_post
    @post = Post.find(params[:id])
  end

  def post_params
    params.require(:post).permit(:title, :content)
  end
end

index.html.haml:

- @posts.each do |post|
  %h2= link_to post.title, post
  %p
    Published at
    = time_ago_in_words(post.created_at)
    by
    = post.user.email

= link_to "New Post", new_post_path

post.rb:

class Post < ActiveRecord::Base
  belongs_to :user
end

user.rb (devise):

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  has_many :posts
end

Here is the GitHub repository: https://github.com/amesbahi/Forum

Thanks!

1 Answer

Hi Alborz,

Alborz Mesbahi , can you update your repo? I found only first initial commit in your github.

Alborz Mesbahi , really, what error message says? migration pending?

alborz
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
alborz
Full Stack JavaScript Techdegree Graduate 30,885 Points

Salman Akram , it says the same error message as the original, and highlights line 7 of the index.html.haml file (post.user.email).

Also the main error message up top of the web app says: NoMethodError in Posts#index.

Strange, I didn't get error message, I am able to create new post successfully and it also shown email address along with time under index.html.haml without any issue.