简体   繁体   中英

undefined method `model_name' for NilClass:Class (Using formtastic in Rails 3.0.3, Ruby 1.9.2)

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.

Trying to make a form with formtastic for a post model, however, I keep getting undefined method `model_name' for NilClass:Class when I try to render the view.

Relevant code:

Demonly_controller.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

Index.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):

  1. <% semantic_form_for @post do |f|%>
  2. <%= f.errors %>
  3. <%= f.inputs do %>
  4. <%= 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

Erm, several problems:

  • You have two controllers and haven't said which is the relevant one
  • PostsController has @post = Post.all outside of a method context
  • Neither of your controllers is setting the plural @posts referenced in your view

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM