Undefined method model_name

I use the default code generated by scaffolding. I have not changed anything.

Showing app/views/presences/_form.html.erb where line #1 raised: undefined method `model_name' for NilClass:Class 1: <%= form_for(@presence) do |f| %> 2: <% if @presence.errors.any? %> 3: <div id="error_explanation"> 4: <h2><%= pluralize(@presence.errors.count, "error") %> prohibited this presence from being saved:</h2> 

What is wrong here? I never call a method called "model_name" and this code is automated, so why doesn't it work?

thanks

+7
source share
2 answers

Try adding this to your presences_controller in new or another relevant action that passes the form:

 #presuming your model is called Presence @presence = Presence.new 
+20
source

The view (and the form_for method) assumes the presence of a real presence model in the @presence variable. The @ -variable variable is passed through the controller, which means you had to set it in the controller action.

In the case of a "new" action - you do not have an existing Presence object that you are playing with (as opposed to, say, "show"), so you just need to create an empty, new one.

In the form_for method, there will be a Presence object similar to this, and: if it exists from db, it will create the correct POST route to update it. But if it is new, empty, it will create the correct route for creating a new one.

Hope this helps ...

+5
source

All Articles