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:

New Activity

<%= render ‘form’ %>

<%= link_to ‘Back’, activities_path %>

and partial:
<%= form_for(@activity) do |f| %>
<% if @activity.errors.any? %>


<%= pluralize(@activity.errors.count, “error”) %> prohibited
this activity from being saved:

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

<% end %>

<%= f.label :activity %>
<%= f.text_field :activity %>
<%= f.label :responsible %>
<%= f.text_field :responsible %>

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:


4:

<%= pluralize(@activity.errors.count, “error”) %>
prohibited this activity from being saved:

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

On 14 April 2012 13:17, Julian P. [email protected] wrote:

end
end

4:

<%= pluralize(@activity.errors.count, “error”) %>
prohibited this activity from being saved:

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?

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 L. 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.

On 14 April 2012 14:11, Julian P. [email protected] wrote:

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


Posted via http://www.ruby-forum.com/.


You received this message because you are subscribed to the Google G. “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.


gplus.to/clanlaw

new.html.erb

New Activity

<%= form_for @activity do |f| %>
<% if @activity.errors.any? %>


<%= pluralize(@activity.errors.count, “error”) %>
prohibited this activity from being saved:

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

New Activity


2:
3: <%= form_for @activity do |f| %>
4: <% if @activity.errors.any? %>
5:

6:

<%= pluralize(@activity.errors.count, “error”) %>
prohibited this activity from being saved:

Colin L. wrote in post #1056501:

On 14 April 2012 13:17, Julian P. [email protected] wrote:

end
end
end

What does the last end match with?

Colin

Damn it that was the mistake. I was always looking at the wrong spot.
Thank you very much Colin.

Julian

On 14 April 2012 15:55, Julian P. [email protected] wrote:

Damn it that was the mistake. I was always looking at the wrong spot.
Thank you very much Colin.

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

On 14 April 2012 13:17, Julian P. [email protected] wrote:

end
end
end

What does the last end match with?

Colin

View:

<%= pluralize(@activity.errors.count, "error") %> prohibited
NoMethodError in Activities#new prohibited this activity from being saved:


You received this message because you are subscribed to the Google G. “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.


gplus.to/clanlaw