undefined method `model_name' for NilClass:Class

Hi guys,
I just started using Ruby on Rails. After implementing the RoR blog
tutorial I started with my own data model.
Sadly I am not able to get my create page running.

Model:
class Activity < ActiveRecord::Base
validates :activity, :presence => true
validates :forKids, :presence => true
validates :start_date, :presence => true
end

Controller:
class ActivitiesController < ApplicationController
  def index
    @activities = Activity.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @activities }
    end
    end
  end

  def new
    @activity = Activity.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @activity }
    end
  end

View:
<h1>New Activity</h1>

<%= render 'form' %>

<%= link_to 'Back', activities_path %>

and partial:
<%= form_for(@activity) do |f| %>
  <% if @activity.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@activity.errors.count, "error") %> prohibited
this activity from being saved:</h2>

      <ul>
      <% @activity.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :activity %><br />
    <%= f.text_field :activity %>
  </div>
  <div class="field">
    <%= f.label :responsible %><br />
    <%= f.text_field :responsible %>
  </div>

I always get the following error message:

NoMethodError in Activities#new

Showing .../app/views/activities/_form.html.erb where line #1 raised:
undefined method `model_name' for NilClass:Class

Extracted source (around line #1):
1: <%= form_for(@activity) do |f| %>
2: <% if @activity.errors.any? %>
3: <div id="error_explanation">
4: <h2><%= pluralize(@activity.errors.count, "error") %>
prohibited this activity from being saved:</h2>

I found several posts about the same error message but so far I wasn't
able to figure out the problem with @activity being nil. Shouldn't it be
initialized by @activity = Activity.new in the controller?

Thanks
Julian

I think the problem is that you have just used
<%= render 'form' %>
to render the form. You have not passed the @activity variable to the
form. Have a look at the Rails Guide on Layouts and Rendering for how
to use :locals to pass variables when rendering a partial.

Colin

Colin Law wrote in post #1056490:

I think the problem is that you have just used
<%= render 'form' %>
to render the form. You have not passed the @activity variable to the
form. Have a look at the Rails Guide on Layouts and Rendering for how
to use :locals to pass variables when rendering a partial.

Colin

Thanks for your response Colin.
I tried to test it without the partial so I replaced the <%= render
'form' %> with the body of the partial but I still get the same error
message.
So the passing of the variable into the partial cannot be the reason for
the problem.

Colin Law wrote in post #1056490:

I think the problem is that you have just used
<%= render 'form' %>
to render the form. You have not passed the @activity variable to the
form. Have a look at the Rails Guide on Layouts and Rendering for how
to use :locals to pass variables when rendering a partial.

Colin

Thanks for your response Colin.
I tried to test it without the partial so I replaced the <%= render
'form' %> with the body of the partial but I still get the same error
message.
So the passing of the variable into the partial cannot be the reason for
the problem.

Put the form back in the view if you really think that and try again.
If you still get the error then post the view and the error message.
Copy and paste, do not retype.

Colin

new.html.erb

<h1>New Activity</h1>

<%= form_for @activity do |f| %>
    <% if @activity.errors.any? %>
        <div id="error_explanation">
          <h2><%= pluralize(@activity.errors.count, "error") %>
prohibited this activity from being saved:</h2>

          <ul>
            <% @activity.errors.full_messages.each do |msg| %>
                <li><%= msg %></li>
            <% end %>
          </ul>
        </div>
    <% end %>

    <div class="field">
      <%= f.label :activity %><br />
      <%= f.text_field :activity %>
    </div>
    <div class="field">
      <%= f.label :responsible %><br />
      <%= f.text_field :responsible %>
    </div>
    <div class="field">
      <%= f.label :mail %><br />
      <%= f.text_field :mail %>
    </div>
    <div class="field">
      <%= f.label :phone %><br />
      <%= f.text_field :phone %>
    </div>
    <div class="field">
      <%= f.label :link %><br />
      <%= f.text_field :link %>
    </div>
    <div class="field">
      <%= f.label :forKids %><br />
      <%= f.text_field :forKids %>
    </div>
    <div class="field">
      <%= f.label :start_date %><br />
      <%= f.text_field :start_date %>
    </div>
    <div class="field">
      <%= f.label :end_date %><br />
      <%= f.text_field :end_date %>
    </div>
    <div class="field">
      <%= f.label :iteration %><br />
      <%= f.text_field :iteration %>
    </div>
    <div class="actions">
      <%= f.submit %>
    </div>
<% end %>

NoMethodError in Activities#new

Showing .../app/views/activities/new.html.erb where line #3 raised:
undefined method `model_name' for NilClass:Class

Extracted source (around line #3):
1: <h1>New Activity</h1>
2:
3: <%= form_for @activity do |f| %>
4: <% if @activity.errors.any? %>
5: <div id="error_explanation">
6: <h2><%= pluralize(@activity.errors.count, "error") %>
prohibited this activity from being saved:</h2>

Hi guys,
I just started using Ruby on Rails. After implementing the RoR blog
tutorial I started with my own data model.
Sadly I am not able to get my create page running.

Model:
class Activity < ActiveRecord::Base
validates :activity, :presence => true
validates :forKids, :presence => true
validates :start_date, :presence => true
end

Controller:
class ActivitiesController < ApplicationController
def index
@activities = Activity.all

respond_to do |format|
format.html # index.html.erb
format.json { render json: @activities }
end
end
end

What does the last end match with?

Colin

Colin Law wrote in post #1056501:

I don't understand why that did not give an error, the controller#new
method should not have been found.

For future similar problems have a look at the Rails Guide on
Debugging. It will show you how to debug the code so that these
problems can be more easily found.

Colin