Rails undefined method `` model_name '' for NilClass: Class

I have a form that I want to display at the top of each page, so I included it in the file /app/views/layouts/application.html.erb and I got the undefined methodmodule_name error for NilClass: Class` when trying to load the page.

Here is a form fragment in application.html.erb

 <%= form_for @user do |f| %>
    <h3>Add new contact</h3>
    <%= f.text_field :first_name %><br />
    <%= f.text_field :last_name %>
    <%= f.text_field :email %>
    <hr />
    <%= f.submit "Add Contact" %>
<% end %>

Here is my / app / controllers / user _controller.rb

class UserController < ApplicationController

    def index

    end

    def new
        @user = User.new
    end

end

I think I'm getting this error because, because the form is in the application.html.erb file, I need to somehow specify the path, but again I am very new to rails.

Let me know if you need anything else.

EDIT Next Ola offer. In my app / views / layouts / application.html.erb file, I have something like this:

<div>
<%= yield :add_user %>
</div>

app/views/users/new.html.erb

<% content_for :add_user do %>
    <%= form_for @user do |f| %>
        <h3>Add new contact</h3>
        First Name<br />
        <%= f.text_field :first_name %><br />
        Last Name<br />
        <%= f.text_field :last_name %><br />
        Email<br />
        <%= f.text_field :email %>
        <hr />
        <%= f.submit "Add Contact" %>
    <% end %>
<% end %>

, url http://localhost:3000/admin/index add_user content_for app/views/admin/index.html.erb

+5
5

(), @user -, form_for ( ).

( Rails) , , , , application.html.erb <%= yield %>. , app/views/users/new.html.erb, . never Rails, , - routes.rb. , , Rails.

: ( UserSessions, ), form_for , :

form_for(User.new)

create

form_for(User.new, :url => { :action => "create" }) 

form_for API Ruby On Rails.

+12

? , , Rails. , . , , ( - ) .

( , . -, http://localhost:3000/users.)

.

NoMethodError in Users # index

/Users/david/dev/testapp/app/views/layouts/application.html.erb, # 11 :

undefined `model_name ' NilClass: Class ( # 11):

8: </head>
9: <body>
10: 
11: <%= form_for @user do |f| %>
12:     <h3>Add new contact</h3>
13:     <%= f.text_field :first_name %><br />
14:     <%= f.text_field :last_name %>

, 11 . - ? , . , . , :

Rails.root://david/dev/testapp

| |

"framework trace", :

activemodel (3.2.6) lib/active_model/naming.rb: 163: in model_name_from_record_or_class' activemodel (3.2.6) lib/active_model/naming.rb:158:in param_key ' actionpack (3.2.6) lib/action_view/helpers/form_helper.rb: 369: in `form_for '

- , , model_name_from_record_or_class, param_key, form_for. !

open_gem, :

gem install open_gem

activemodel, :

gem open activemodel

:

def. * model_name_from_record_or_class

(? - , , self.)

162 naming.rb.

def self.model_name_from_record_or_class(record_or_class)
  (record_or_class.is_a?(Class) ? record_or_class : convert_to_model(record_or_class).class).model_name
end

, ... . ? , , @user, . , , - .

, .

+10

Someth, , @user , , . , before_filter ApplicationController, ..

class ApplicationController < ActionController::Base
  before_filter :initialize_user

  def initialize_user
    @user = User.new
  end
end

OR by creating a new model in the form for the assistant itself (note: this is ugly in terms of separation of problems, but it works ...)

 <%= form_for User.new do |f| %>
   <h3>Add new contact</h3>
   <%= f.text_field :first_name %><br />
   <%= f.text_field :last_name %>
   <%= f.text_field :email %>
   <hr />
   <%= f.submit "Add Contact" %>
 <% end %>
+6
source

Your problem is that you are trying to add a form to each action on your page, but you have not initialized the user in all actions. Try to initialize the user in all actions:

class UserController < ApplicationController
    before_filter :initialize_user

    def index

    end

    def initialize_user
        @user = User.new
    end

end
0
source

in usercontroller:

def index
   @user = User.new
end
0
source

All Articles