undefined method `model_name' for NilClass:Class in rails 3.0.0

Hi,

      i got search which shows following error

       ActionView::Template::Error (undefined method `model_name' for
NilClass:Class):
    1: <%= form_for(@employee) do |e| %>
    2: EMP ID<%= e.text_field :id %><br>
    3: <%= e.submit 'search', :controller => 'employees', :action =>
'search1' %>
    4: <% end %>
in my search action i dint provide anything like
def search
end

if i use <%= form_for(:employee) do |e| %>
it shows no error but the data is moving in to my "show" action

   could any one provide me a soultion ?

Hi,

  i got search which shows following error



   ActionView::Template::Error (undefined method `model_name' for

NilClass:Class):

i think this happens when @employee is nil. double check that @employee is not nil.

Hi Jim,

    I got nothing in my action search and linked the search button to
search1 action, which contains the find function is that a problem?

thanks,
-pab

Pab wrote in post #1019703:

Hi Jim,

    I got nothing in my action search and linked the search button to
search1 action, which contains the find function is that a problem?

Yes. Here is the order of what happens:

1) The browser sends a request that hits the action connected to your
view.
2) The action executes and the view containing your form is processed by
rails and sent to the browser.
3) All variables in your app and their values are destroyed.
4) The user fills out the form and clicks the submit button, which sends
the request to your search1 action, causing the code in search1 to
execute.

So any code in the search1 action executes long after rails processes
the view. In your view, you referenced the @employee variable, so that
view's action has to assign a value to @employee.

Hi,

def search

end

  def search1

            @employee = Employee.find(params[:id])
            respond_to do |format|
              format.html{render :partial => 'show'}
              format.xml
            end

  end

search.html is
<%= form_for(@employee) do |e| %>
EMP ID<%= e.text_field :id %><br>
<%= e.submit 'search', :controller => 'employees', :action =>
'search1' %>
<% end %>

problem is its not showing up search page itself

thanks,
-pab

Hi,

def search

end

def search1

        @employee = Employee.find(params[:id])

        respond_to do |format|

          format.html{render :partial => 'show'}

          format.xml

        end

end

search.html is
<%= form_for(@employee) do |e| %>

EMP ID<%= e.text_field :id %>

<%= e.submit ‘search’, :controller => ‘employees’, :action =>

‘search1’ %>

<% end %>

the search method doesn’t declare any instance variable but in the view template,

you want to use @employee. form_for expects @employee to be declared so

that form_for can form the url and the input field names. so the solution to your

problem is to just declare an @employee in the search action.

hi

def search
@employee
end

i have done the above, but it shows following error

undefined method `model_name' for NilClass:Class

Extracted source (around line #1):

1: <%= form_for(@employee) do |e| %>
2: <%= e.error_msg %>
3: EMP ID<%= e.text_field :id %><br>
4: <%= e.submit 'search', :controller => 'employees', :action =>
'search1' %>

thanks,
-pab

hi

def search

@employee

end

i have done the above, but it shows following error

undefined method `model_name’ for NilClass:Class

Extracted source (around line #1):

1: <%= form_for(@employee) do |e| %>

2: <%= e.error_msg %>

3: EMP ID<%= e.text_field :id %>

4: <%= e.submit ‘search’, :controller => ‘employees’, :action =>

‘search1’ %>

you need to set @employee to something, not just declare it like that in the controller.

@employee = Employee.new #or some other model

Hi,

in @employee = Employee.new, what .new describes about? and i gave
@employee = Employee.search
instead of that, which result in error

  undefined method `search' for #<Class:0x9295500>

thanks,
-pab

Hi,

in @employee = Employee.new, what .new describes about? and i gave

@employee = Employee.search

instead of that, which result in error

undefined method `search’ for #Class:0x9295500

I suggest you read some tutorials first Pab