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 trialAllen Lamphear
Courses Plus Student 6,660 PointsDevise 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
6,079 PointsMost 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.
Michael Hall
Courses Plus Student 30,909 PointsThis 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.
Allen Lamphear
Courses Plus Student 6,660 PointsThanks 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.
Allen Lamphear
Courses Plus Student 6,660 PointsAllen Lamphear
Courses Plus Student 6,660 PointsOMG! 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!