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: <div id="errorExplanation">
4: <h2><%= pluralize(@user_comment.errors.count, "error") %>
prohibited this user comment from being saved:</h2>
(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?
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: <div id="errorExplanation">
4: <h2><%= pluralize(@user_comment.errors.count, "error") %>
prohibited this user comment from being saved:</h2>
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.
I had tried that already. My _form.html.erb actually starts with
<%= form_for(@user_comment) do |user_comment| %>
<% if @user_comment.errors.any? %>
<div id="errorExplanation">
<h2><%= pluralize(@user_comment.errors.count, "error") %>
prohibited this user comment from being saved:</h2>
and I get the same results:
Showing c:/Gpsappm/app/views/user_comments/_form.html.erb where line #1
raised:
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: <div id="errorExplanation">
4: <h2><%= pluralize(@user_comment.errors.count, "error") %>
prohibited this user comment from being saved:</h2>
(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.
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.
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.
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!
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.
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 new 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]).