NilClass:Class 的未定义方法“model_name”(在 Rails 3.0.3、Ruby 1.9.2 中使用 formtastic)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4724983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
undefined method `model_name' for NilClass:Class (Using formtastic in Rails 3.0.3, Ruby 1.9.2)
提问by acrognale
A tad bit confused as to why this isn't working. I'm using Ruby 1.9.2 with Rails 3.0.3 on Windows 7.
关于为什么这不起作用有点困惑。我在 Windows 7 上使用 Ruby 1.9.2 和 Rails 3.0.3。
Trying to make a form with formtastic for a post model, however, I keep getting undefined method `model_name' for NilClass:Classwhen I try to render the view.
尝试为后期模型制作带有 formtastic 的表单,但是,当我尝试呈现视图时,我一直为 NilClass:Class获取未定义的方法“model_name”。
Relevant code:
相关代码:
Demonly_controller.rb
Demonly_controller.rb
class DemonlyController < ApplicationController
def index
@post = Post.all
end
end
Posts_controller.rb
Posts_controller.rb
class PostsController < ApplicationController
end
Post.rb
后.rb
class Post < ActiveRecord::Base
attr_accessible :title, :post, :date, :time, :user, :visible, :comments
end
Index.html.erb
索引.html.erb
<h1>Demonly</h1>
<% semantic_form_for @post do |f|%>
<%= f.errors %>
<%= f.inputs do %>
<%= f.input :title %>
<%= f.input :post %>
<%= f.input :date %>
<%= f.input :time %>
<%= f.input :user %>
<%= f.input :visible %>
<%= f.input :comments %>
<% end %>
<% end %>
It's rather likely that I'm doing something very stupid seeing as I'm sick and mentally cloudy.
很可能我正在做一些非常愚蠢的事情,因为我生病了,精神上很模糊。
Extracted source (around line #2):
提取的源代码(围绕第 2 行):
- <% semantic_form_for @post do |f|%>
- <%= f.errors %>
- <%= f.inputs do %>
- <%= f.input :title %>
- <% semantic_form_for @post do |f|%>
- <%= f.errors %>
- <%= f.inputs 做 %>
- <%= f.input :title %>
Let me know if anything else is needed.
让我知道是否需要其他任何东西。
EDIT: Forgot to change some things back.
编辑:忘了改回一些东西。
Forgot to include the db schema:
忘记包含数据库架构:
create_table "posts", :force => true do |t|
t.string "title"
t.text "post"
t.datetime "date"
t.datetime "time"
t.string "user"
t.boolean "visible"
t.boolean "comments"
t.datetime "created_at"
t.datetime "updated_at"
end
回答by noodl
Erm, several problems:
嗯,几个问题:
- You have two controllers and haven't said which is the relevant one
- PostsController has
@post = Post.alloutside of a method context - Neither of your controllers is setting the plural
@postsreferenced in your view
- 你有两个控制器,但没有说哪个是相关的
- PostsController
@post = Post.all在方法上下文之外 - 您的控制器都没有设置
@posts您视图中引用的复数

