3

有点困惑为什么这不起作用。我在 Windows 7 上使用 Ruby 1.9.2 和 Rails 3.0.3。

但是,尝试为 post 模型制作带有formtastic 的表单时,当我尝试渲染视图时,我不断收到NilClass:Class 的未定义方法“model_name” 。

相关代码:

恶魔控制器.rb

class DemonlyController < ApplicationController
    def index
      @post = Post.all
    end
end

Posts_controller.rb

class PostsController < ApplicationController

end

Post.rb

class Post < ActiveRecord::Base
    attr_accessible :title, :post, :date, :time, :user, :visible, :comments
end

索引.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 %>

很可能我正在做一些非常愚蠢的事情,因为我生病了,精神混乱。

提取的源代码(在第 2 行附近):

  1. <% semantic_form_for @post do |f|%>
  2. <%= f.errors %>
  3. <%= f.inputs 做 %>
  4. <%= f.input :title %>

让我知道是否需要其他任何东西。

编辑:忘记改回一些东西。

忘记包含 db 架构:

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
4

1 回答 1

8

呃,几个问题:

  • 你有两个控制器,但没有说哪个是相关的
  • PostsController@post = Post.all在方法上下文之外
  • @posts您的两个控制器都没有设置您视图中引用的复数
于 2011-01-18T14:10:21.210 回答