Topic: You have a nil object when you didn't expect it!

Hey all! i am new to ROR and i am trying to make a registration page using a tutorial and i am getting the following error whenever i try to run  my page:

it says:
Showing C:/rails/pbbg/app/views/account/registration.html.erb where line #1 raised:

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]

Extracted source (around line #1):

1: <% form_for :user, @user do |f| %>
2:   <%= error_messages_for :user %>
3:   Username: <%= f.text_field :username %><br/>
4:   Password: <%= f.password_field :password %><br/>


the link to the tutorial is

http://buildingbrowsergames.com/2008/08 … -on-rails/

Any suggestion would be really helpful

thanks in advance
amit rawat

Re: You have a nil object when you didn't expect it!

Change the tag of 'form_for' helper as folows:

<%= form_for ...%>
...

Re: You have a nil object when you didn't expect it!

I have done what you said and now it show another error:

undefined method `model_name' for NilClass:Class

Re: You have a nil object when you didn't expect it!

I think you're missing "@user = User.new" in the registration method.

def registration
    @user = User.new   

    if request.post? and params[:user]
        @user = User.new(params[:user])
        if @user.save
            flash[:notice] = "User created."
            redirect_to :action => "index"
        end
    end
end

see Rails Form Helper Guides > 2.2 Binding a Form to an Object

Last edited by ToddG (2011-10-10 18:44:53)