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 %>

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 ?

On Wed, Aug 31, 2011 at 5:53 PM, Pab [email protected] wrote:

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.

it shows no error but the data is moving in to my “show” action
http://groups.google.com/group/rubyonrails-talk?hl=en.

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 is routed to the action that
    corresponds to your view with the form.
  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 containing your form. 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 %>

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

problem is its not showing up search page itself

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’ %>

thanks,
-pab

On Fri, Sep 2, 2011 at 5:30 PM, Pab [email protected] wrote:

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.

“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 this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.

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

On Fri, Sep 2, 2011 at 6:31 PM, Pab [email protected] wrote:

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

For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.

On Fri, Sep 2, 2011 at 7:00 PM, Pab [email protected] wrote:

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

For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.