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
Shaun Jackson
1,270 PointsIssue with Name Error
Please see the following code.
NameError in StatusesController#create
uninitialized constant Status::UserId
app/controllers/statuses_controller.rb:43:in `new'
app/controllers/statuses_controller.rb:43:in `create'
Request
Parameters:
{"utf8"=>"?",
"authenticity_token"=>"JbWbJPHKd3QFtWlbRGf4CH+TYnyUa2prSTgAdlAgwtw=",
"status"=>{"user_id"=>"3",
"content"=>"BLAH BLAH BLAH"},
"commit"=>"Create Status"}
Below is a copy of the status controller:
class StatusesController < ApplicationController
# GET /statuses
# GET /statuses.json
def index
@statuses = Status.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @statuses }
end
end
# GET /statuses/1
# GET /statuses/1.json
def show
@status = Status.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @status }
end
end
# GET /statuses/new
# GET /statuses/new.json
def new
@status = Status.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @status }
end
end
# GET /statuses/1/edit
def edit
@status = Status.find(params[:id])
end
# POST /statuses
# POST /statuses.json
def create
@status = Status.new(params[:status])
respond_to do |format|
if @status.save
format.html { redirect_to @status, notice: 'Status was successfully created.' }
format.json { render json: @status, status: :created, location: @status }
else
format.html { render action: "new" }
format.json { render json: @status.errors, status: :unprocessable_entity }
end
end
end
# PUT /statuses/1
# PUT /statuses/1.json
def update
@status = Status.find(params[:id])
respond_to do |format|
if @status.update_attributes(params[:status])
format.html { redirect_to @status, notice: 'Status was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @status.errors, status: :unprocessable_entity }
end
end
end
# DELETE /statuses/1
# DELETE /statuses/1.json
def destroy
@status = Status.find(params[:id])
@status.destroy
respond_to do |format|
format.html { redirect_to statuses_url }
format.json { head :no_content }
end
end
end
5 Answers
Alan Johnson
7,625 PointsTry changing
belongs_to :user_id
to:
belongs_to :user
Just as a small aside, when posting code segments to the forum, if you use three back ticks (`) on a line above and below the code (that's called a code fence in markdown) it'll format your code nicely.
Alan Johnson
7,625 PointsCan you share your Status model code? It looks like the issue may be caused by something there.
Thanks!
Shaun Jackson
1,270 Pointsclass Status < ActiveRecord::Base
attr_accessible :content, :user_id
belongs_to :user_id
end
Shaun Jackson
1,270 PointsThat worked. What the hell! That one little bit messes up the whole coding!
Better start writing these down.
Alan Johnson
7,625 PointsIt's crazy, but it's how it works. When you use belongs_to :model it's saying that there's an attribute called model_id on the current class that relates to the id attribute on the model named Model. Rails does a lot by convention, so it can take a little time to get used to, but once you do it's a huge time saver.
Shaun Jackson
1,270 PointsThanks for your help I'll remember the code framing for next time, which might be soon ;)
Alan Johnson
7,625 PointsAny time! And definitely post here if we can help out in any way. Can't wait to see what you build with your new Rails skills
Shaun Jackson
1,270 PointsYep see, another bug already?
undefined method `full_name' for nil:NilClass
17: <li class="active"><%= link_to "Log Out", destroy_user_session_path %></li>
18: </ul>
19: <ul class="nav pull-right">
20: <li><%= link_to current_user.full_name, "#" %></li>
21: </ul>
22: </div>
23: </div>
Here is the full code from application layout:
<!DOCTYPE html>
<html>
<head>
<title>Treebook</title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>
<div class="navbar">
<div class="navbar-inner">
<a class="brand" href="#">Faceown</a>
<ul class="nav">
<li class="active"><%= link_to "New Ownage", new_status_path %></li>
<li class="active"><%= link_to "All Statuses", statuses_path %></li>
<li class="active"><%= link_to "Sign Up", new_user_registration_path %></li>
<li class="active"><%= link_to "Log Out", destroy_user_session_path %></li>
</ul>
<ul class="nav pull-right">
<li><%= link_to current_user.full_name, "#" %></li>
</ul>
</div>
</div>
<div class="container">
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
<%= yield %>
</div>
</body>
</html>
Alan Johnson
7,625 PointsIf you could, go ahead and start up a new thread on this so we can make sure others can see the answer when they run into it also - it's definitely a different issue.
And note how I tweaked your post formatting to insert the code fences and get the code to highlight.