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!

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


Devise User Info From Comments


I can't seem to get the user information from a comment. I get an error undefined method `user' for Note:0x007fcbe935afe0

ie. Added by "user"

I have a notes model that consist of

id: integer, 
note: text, 
created_at: datetime, 
updated_at: datetime, 
title: string, 
user_id: integer

Notes Create Controller

def create
    @tablet = Tablet.find(params[:tablet_id])
    @note = @tablet.notes.build(params[:note])
    @note.user_id = current_user.id

    # render plain: params[:note].inspect

    respond_to do |format|
      if @note.save
        format.html { redirect_to tablet_path(@tablet), notice: 'Note was successfully created.' }
        format.json { render :show, status: :created, location: @note }
        format.html { render :new }
        format.json { render json: @note.errors, status: :unprocessable_entity }

def note_params


<div class="panel panel-body">
      <h3>Add a note:</h3>
      <%= form_for([@tablet, @tablet.notes.build]) do |f| %>
        <div class="form-group">
          <%= f.label :title, "Title", class: "control-label" %>
          <%= f.text_field :title, class: "form-control" %>

        <div class="form-group">
          <%= f.label :note, "Note", class: "control-label" %>
          <%= f.text_field :note, class: "form-control" %>

        <div class="form-group">
          <%= f.submit class: "btn btn-sm btn-success" %>
      <% end %>


      <% @tablet.notes.limit(10).each do |n| %>
      <h4><%= n.title %></h4>
      <p class="text-muted">Added by <strong><%= n.user.user_name %></strong> on <%= l(n.created_at, format: '%B, %d %Y %H:%M:%S') %></p>

        <p><%= n.note %></p>

        <%= link_to 'Delete Note', [n.tablet, n],
                    method: :delete,
                    data: { confirm: 'Are you sure you want to delete this note?' },
                    class: "btn btn-xs btn-danger" %>
      <% end %>

Let me know what I am doing wrong or if you need more information.


Update: Might need to see the models

class Note < ActiveRecord::Base
  belongs_to :tablet
class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :authentication_keys => [:user_name]
  has_many :tablets
  has_many :notes
class Tablet < ActiveRecord::Base
  has_many :notes
  has_one :inventory
  belongs_to :user
  validates :alias, :email, presence: true
  validates :alias, uniqueness: { message: " is already taken." }

3 Answers

Jesse James
Jesse James
6,079 Points

Most likely the issue here is that you're not declaring that notes belong to users in the Notes model. When Rails goes to look up the association on your n.user.user_name call it gets a bit wonky since you've only specified half of the association (the User model portion). Add belongs_to :user to your Notes model and let us know if that helped at all.

OMG! Thank you so much! What a simple thing to over look! I guess I need to brush up on my associations. Dang. Thanks Jesse James and Michael Hall for responding and all the help!

Michael Hall
Michael Hall
Courses Plus Student 30,909 Points

This is where it's failing correct? ```Added by <strong><%= n.user.user_name %></strong>

I wish I could look at this and give you an answer, but I always have to poke around to find the answer. A great tool for this is pry.  gem 'pry' in your gemfile. Then place  <%= binding.pry %> in  the line before the error. in the console enter n and see what is available to note.  You could also place binding.pry before @note.user.id = current_user.id, and check what the value of current_user is. I hope that helps.

Thanks Michael Hall for responding but that "pry" looks a little advanced for me. :-) Couldn't really figure out how to do what you were saying. But I will keep messing with it to figure it out. Hopefully I figure it out or someone can figure it out faster. Again, thanks a lot.