Undefined method `model_name' for NilClass:Class

I am new to Ruby on Rails and have a very simple foreign key example
that is driving me nuts. I am using Rails 3 under Windows Vista and have
two tables: users and user_comments. user_comments.user_id should
point to the user a comment is about.

In my show view for user I have a link to the new method of
user_comments to allow a comment to be created. This throws:

Showing c:/Gpsappm/app/views/user_comments/_form.html.erb where line #1
raised:

undefined method `model_name’ for NilClass:Class
Extracted source (around line #1):

1: <%= form_for(@user_comment) do |user_comments| %>
2: <% if @user_comment.errors.any? %>
3:


4:

<%= pluralize(@user_comment.errors.count, “error”) %>
prohibited this user comment from being saved:

(1) What exactly does this error message mean? From other posts I
gather this is a null instance of UserComments, but this is to create a
new instance?

(2) What are the step-by-step methods I should follow to debug such
issues?

Thanks!

On Jan 26, 2011, at 5:48 PM, David G. wrote:

#1
raised:

undefined method `model_name’ for NilClass:Class
Extracted source (around line #1):

1: <%= form_for(@user_comment) do |user_comments| %>
2: <% if @user_comment.errors.any? %>
3:


4:

<%= pluralize(@user_comment.errors.count, “error”) %>
prohibited this user comment from being saved:

If this is a copy and paste from your result, then look at the block
– you have user_comments (plural) in there, but you refer to
user_comment inside the block.

Walter

I had tried that already. My _form.html.erb actually starts with

<%= form_for(@user_comment) do |user_comment| %>
<% if @user_comment.errors.any? %>


<%= pluralize(@user_comment.errors.count, “error”) %>
prohibited this user comment from being saved:

and I get the same results:

Showing c:/Gpsappm/app/views/user_comments/_form.html.erb where line #1
raised:

undefined method `model_name’ for NilClass:Class

David G. wrote in post #977740:

I am new to Ruby on Rails and have a very simple foreign key example
that is driving me nuts. I am using Rails 3 under Windows Vista and have
two tables: users and user_comments. user_comments.user_id should
point to the user a comment is about.

In my show view for user I have a link to the new method of
user_comments to allow a comment to be created. This throws:

Showing c:/Gpsappm/app/views/user_comments/_form.html.erb where line #1
raised:

undefined method `model_name’ for NilClass:Class
Extracted source (around line #1):

1: <%= form_for(@user_comment) do |user_comments| %>
2: <% if @user_comment.errors.any? %>
3:


4:

<%= pluralize(@user_comment.errors.count, “error”) %>
prohibited this user comment from being saved:

(1) What exactly does this error message mean? From other posts I
gather this is a null instance of UserComments, but this is to create a
new instance?

(2) What are the step-by-step methods I should follow to debug such
issues?

Thanks!

Post your whole _form.html.erb file. I suspect the error is elsewhere,
and Rails is pointing to line 1 because that’s where the enclosing block
starts.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Sent from my iPhone

I have attached it - thanks! I am calling this from from a link in the
show of the User class so have enclosed that view as well. Thanks!

Please quote when replying.

David G. wrote in post #978047:

I have attached it - thanks! I am calling this from from a link in the
show of the User class so have enclosed that view as well. Thanks!

I’m not sure if this is the direct cause, but it looks like the variable
f isn’t defined anywhere. Normally that would be the block argument
that form_for takes, but you’ve changed the name of that block argument
to user_comment, without correspondingly changing references to it.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Sent from my iPhone

Thanks. Same results. So I am also attaching the
user_comments\new.html.erb file. “Show” of User should let you create a
new comment.

ActionView::Template::Error (undefined method `model_name’ for
NilClass:Class):
1: <%= form_for(@user_comment) do |user_comment| %>

2:   <% if @user_comment.errors.any? %>

3:     <div id="errorExplanation">

4:       <h2><%= pluralize(@user_comment.errors.count, "error") %>

prohibited this user comment from being saved:

app/views/user_comments/_form.html.erb:1:in
_app_views_user_comments__form_html_erb__805116599_31946496__132227956' app/views/user_comments/new.html.erb:3:in_app_views_user_comments_new_html_erb__406286635_32110236__1065675980’
app/controllers/user_comments_controller.rb:15:in `new’

I would think, @user_comment is nil. Could you attach your controller,
too?

Sure and thanks! You are probably right, since this is a new
UserComment. I am attaching the two controllers - the other files are
included above.

David G. wrote in post #978220:

Sure and thanks! You are probably right, since this is a new
UserComment. I am attaching the two controllers - the other files are
included above.

Well, there’s your problem! Look at the UserComment#new method. You’re
setting @usercomment in the controller, then trying to read
@user_comment in the view.

BTW, I would suggest changing the name of the class to Comment.
UserComment is probably redundant.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Thanks for your reply. So let me ask a few specific questions to
clarify my understanding of Ruby.

class UserCommentsController < ApplicationController
def new
@usercomment = UserComment.new

respond_to do |format|
  format.html # new.html.erb
  format.xml  { render :xml => @usercomment }
end

end

I would have thought, given that this was scaffolding, that
UserComment.new allocated the space for the UserComment, assigned the
values to nil, and that the view would render them as empty data. What
IS this really doing? I assumed it passes the usercomment to one of to
format methods that are internal to Ruby on Rails. Thanks again!

Marnen Laibow-Koser wrote in post #978225:

David G. wrote in post #978220:

Sure and thanks! You are probably right, since this is a new
UserComment. I am attaching the two controllers - the other files are
included above.

Well, there’s your problem! Look at the UserComment#new method.

Er, sorry, I meant the UserCommentsController#new method.

You’re
setting @usercomment in the controller, then trying to read
@user_comment in the view.

BTW, I would suggest changing the name of the class to Comment.
UserComment is probably redundant.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Hi,

Within your view: I would say you need to replace “do |user_comment|”
with something else like “do |u|”

There are no errors in your controller for the new action.

I see you said:

““Show” of User should let you create a
new comment.”

Part of the Rails framework is the convention of mapping the view to
the controller action. Example:
new.html.erb will be mapped to new action in controller
show.html.erb will be mapped to show action in controller.

If you are trying to get your new.html.erb to call the show action in
your controller, then this will be the cause of your error, as in show
action in the controller, you can see that the action is expecting a
UserComment to be there already by finding by id:
UserComment.find(params[:id]).

This will lead to a null error.

You also want to make sure that @food has an id of some kind, or Rails
complains. For the ‘new’ action, you want to set @food.id to nil to make
sure the form works for the new action.

You might not think this is important, but it is. If you decide to move
this
controller into a namespace for example, you are introducing a potential
problem where the new or edit form doesn’t work anymore. I have found it
less trouble to actually test it to make sure Rails doesn’t throw some
error
about trying to find a ‘show’ action when it’s not supposed to.

Ken

Again: Please quote when replying! Otherwise the discussion will be
hard to follow, and you will be more likely to skip over things in the
posts you’re trying to respond to (as you seem to have done in this
case).

David G. wrote in post #978245:

Thanks for your reply. So let me ask a few specific questions to
clarify my understanding of Ruby.

class UserCommentsController < ApplicationController
def new
@usercomment = UserComment.new

respond_to do |format|
  format.html # new.html.erb
  format.xml  { render :xml => @usercomment }
end

end

I would have thought, given that this was scaffolding, that
UserComment.new allocated the space for the UserComment, assigned the
values to nil, and that the view would render them as empty data. What
IS this really doing?

It’s constructing a UserComment object (not necessarily assigning the
values to nil, but running the constructor method).

I assumed it passes the usercomment to one of to
format methods that are internal to Ruby on Rails.

Huh?

Thanks again!

You completely missed my point from my last post. Again, I wrote:

You’re
setting @usercomment in the controller, then trying to read
@user_comment in the view.

See the issue there? If not, then you need to brush up on your powers
of observation before attempting to program in any language.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

I just found it bothersome to use stubs when testing rails controllers.

A far easier way to do this is to define what @food is in a
before(:each)
block and then use it in your tests:

describe “GET edit” do
it “should assign the requested food to @food” do
Food.should_receive(:find).with(“1”).and_return(@food)

  get :edit, :id => "1"

  assigns(:food).should be(@food)
end

end

This is still very fast, and it has the added benefit that you can use
“render_views” at the top of the spec to test your routes, erb code,
etc. ->
which is REALLY valuable.

Ken

On Tue, May 31, 2011 at 2:38 PM, Chris H. [email protected]
wrote:

Still getting the same error. ugggh.

Truly, you have to give us some more code to help you.

Still getting the same error. ugggh.

Full Error:

Failures:

  1. FoodsController should assign the requested food to @food
    Failure/Error: get :edit, :id => @food.id
    ActionView::Template::Error:
    undefined method `model_name’ for NilClass:Class

    ./app/views/foods/_form.html.erb:1:in

_app_views_foods__form_html_erb__2500079613968748019_2175296860__3835516174707068795' # ./app/views/foods/edit.html.erb:3:in_app_views_foods_edit_html_erb__1881942591214137850_2175310520__4134993942280106228’
# ./app/controllers/foods_controller.rb:30:in edit' # ./spec/controllers/foods_controller_spec.rb:21:inblock (2
levels)
in <top (required)>’

On Tue, May 31, 2011 at 2:57 PM, Chris H. [email protected]
wrote:

end

#describe “stub_model(Food) with a hash of stubs” do
#let(:food) do
# stub_model Food, :id => 5, :food =>{:name => ‘brisket’}
#end

In my earlier post, I’ve already told you not to use mocks for your
models.
It’s more hassle than it is worth.

before(:each) do
@food = Food.new
@food.id = 1
end

describe “GET ‘edit’” do
it “should be successful” do
Food.stub(:find).with(“1”).and_return(@food)

  get :edit

  assigns(:food).should == @food
end

end

^ This should work.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs