js111
js111

Reputation: 1314

Getting a NoMethodError

Im getting this error:

Showing /Users/nelsonkeating/Desktop/imright/app/views/users/new.html.erb where line #4 raised:

undefined method `model_name' for NilClass:Class Extracted source (around line #4):

4: <%= form_for(@user) do |f| %>

5:   <div class="field">
6:     <%= f.label :name %><br />
7:     <%= f.text_field :name %>

what do i change to fix this?

Upvotes: 0

Views: 193

Answers (3)

daya
daya

Reputation: 11

try to view page <%= form_for @user, :url=> {:action =>"create"} do |f| %> in controller def new @user = User.new end

def create
  @user = User.new(params[:user])

           if @user.save
        flash[:notice] = "Registration successful."
           redirect_to(:controller =>"user_sessions", :action =>"new")
     else
            render :action => 'new'
     end

end

Upvotes: 1

Mike Lewis
Mike Lewis

Reputation: 64177

@user does not exist then (Look in the error because it is saying that it is a NilClass).

You either want to do:

form_for(User.new) do |f|

or set @user in the controller;

class UsersController
  def new
    @user = User.new
  new
end

I suggest the latter because it's a rule of thumb for MVC to not put model calls in your views.

Upvotes: 2

bioneuralnet
bioneuralnet

Reputation: 5301

That indicates that @user is nil. Perhaps you changed the variable name from @user to something else in your controller and forgot to change it in your view? Maybe this view is being rendered in the create or update action after an error, and the variable is called something besides @user in those actions?

Upvotes: 0

Related Questions