Undefined method error

I have been working on a project based on Michael H.‘s book Ruby on
Rails Tutorial in order to try to teach myself Ruby on Rails. However,
when it tries to render a form designed to allow a user to submit a
small message, I get an error saying, "undefined method `model_name’ for
NilClass:Class". It says the error is at line 1, which is “<%= form_for
@micropost do |f| %>”. I have the following defined in
app/controllers/microposts_controller.rb:

def new
@micropost = Micropost.new
end

I would have expected this code to create a blank Micropost object that
gets passed to form_for, but that doesn’t seem to be what happened. I
attached a few files for reference.

when you want to create a blank instance of a model for a form’s sake
like
this
scenario, you should create that blank instance in the controller’s
action
which
leads to that specific view you’re rendering the form in.

As I recall in Michael H.'s Ruby on Rails Tutorial sample application
you
rendered the _micropost_form partial in the Page’s controller
home.html.erb
view which means you’ll go to that view through the home action in the
PagesController and for handling this nil problem you should put that
line of code (@micropost = Micropost.new) in the home action of the
pages controller.

Hope that helps.
Good Luck :wink:

On Thu, Jul 19, 2012 at 1:29 AM, One T. [email protected] wrote:

end


Sam Serpoosh
Software Developer: http://masihjesus.wordpress.com
Twitter @masihjesus http://twitter.com/masihjesus

Thank you for the help. Putting “@micropost = Micropost.new” in
pages_controller.rb solved this error. However, I now have a new error
which states “undefined method `humanize’ for nil:NilClass”. It says the
error is at the line “<%= f.label @user.email %>
”. I defined @user
= User.new in pages_controller.rb.

I am doing this a little bit differently from Michael H.'s book. I
want to be able to submit on one form the micropost content, user e-mail
address and password instead of having the user logged in.

Sam Serpoosh wrote in post #1069239:

when you want to create a blank instance of a model for a form’s sake
like
this
scenario, you should create that blank instance in the controller’s
action
which
leads to that specific view you’re rendering the form in.

As I recall in Michael H.'s Ruby on Rails Tutorial sample application
you
rendered the _micropost_form partial in the Page’s controller
home.html.erb
view which means you’ll go to that view through the home action in the
PagesController and for handling this nil problem you should put that
line of code (@micropost = Micropost.new) in the home action of the
pages controller.

Hope that helps.
Good Luck :wink:

On Thu, Jul 19, 2012 at 1:29 AM, One T. [email protected] wrote:

end


Sam Serpoosh
Software Developer: http://masihjesus.wordpress.com
Twitter @masihjesus http://twitter.com/masihjesus

Ok, of course you can do this. The thing that you should pay attention
to
is that when the user fill the information in your special form in the
home
page (including user_email, user_password, micropost_content) you should
authorize the user first by his/her user_email and password and the
combination
of those two is ok then you create a new micropost by that content and
associate
it to the user that you authorized at this point you created a complete
micropost
with its user association.

But be careful about using the form and it’s labels and fields cause in
this
scenario you did the “@user = User.new” and @user.email is nil and then
you have this line → “<%= f.label @user.email” %>" so rails tried to
create
some id and name for the label field using the argument you provided
(@user.email)
and it calls the humanize method on it for some string generation and
replacing white
spaces with underscores (which doesn’t matter if you don’t know exactly
at
this point)
and according to the fact that @user.email is nil then the humanize
method
on it will
throw the “undefined method on nil” exception.

you should change the argument to the f.label accordingly and be careful
about it.

Hope that helps.
Good Luck :wink:

On Thu, Jul 19, 2012 at 2:11 AM, One T. [email protected] wrote:

you
On Thu, Jul 19, 2012 at 1:29 AM, One T. [email protected] wrote:
Twitter @masihjesus http://twitter.com/masihjesus
“Ruby on Rails: Talk” group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Sam Serpoosh
Software Developer: http://masihjesus.wordpress.com
Twitter @masihjesus http://twitter.com/masihjesus