开发者

undefined method model_name

开发者 https://www.devze.com 2023-03-14 06:59 出处:网络
I\'m using the default code created by scaffolding. I haven\'t changed anything. Showing app/views/presences/_form.html.erb where line #1 raised:

I'm using the default code created by scaffolding. I haven't changed anything.

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

1: <%= form_for(@prese开发者_如何学编程nce) 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'm never calling a method called "model_name" and this code is automated, so why doesn't it work?

Thanks


Try adding this to your presences_controller in the new or other relevant action that is rendering the form:

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


The view (and the form_for method) expect to actually have a real Presence model in the @presence variable. An @-variable like this is passed through from the controller, which means that you had to set it up in the controller action.

In the case of the "new" action - you don't have an existing Presence object that you are playing with (unlike, say "show") - so you need to just set up a blank, new one.

The form_for method will take a Presence object like this and: if it's an existing one from the db, will create the correct POST-route to update it. But if it's a new, empty one, will create the correct route for creating a new one.

Hope that helps...

0

精彩评论

暂无评论...
验证码 换一张
取 消