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.
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?
<%= 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.
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.
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.
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.