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

Devise User Info From Comments

All,

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, 
tablet_id:integer
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
    @note.save

    # 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 }
      else
        format.html { render :new }
        format.json { render json: @note.errors, status: :unprocessable_entity }
      end
    end
  end

private
def note_params
      params.require(:note).permit(:note,:title)
    end

Tablet#Show

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

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

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

     <br>

      <h3>Notes</h3>
      <hr>
      <% @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>

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

      <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" %>
      </p>
      <% end %>
    </div>

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

Thanks!

Update: Might need to see the models

class Note < ActiveRecord::Base
  belongs_to :tablet
end
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
end
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." }
end

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