2

I'm doing the Getting started with Rails tutorial and when I run the local server from shell I get this:

`NoMethodError in Posts#new` `/_form.html.erb where line #1 raised:
`undefined method `model_name' for NilClass:Class

That is the extracted source (around line #1):

1: <%= form_for @post do |f| %>
2:  <% if @post.errors.any? %>
3:  <div id="errorExplanation">
4:      <h2><%= pluralize(@post.errors.count, "error") %> prohibited

I just started on Ruby on Rails and I can't figure out what is happening. What am I doing wrong?

Mischa
  • 42,876
  • 8
  • 99
  • 111
Hell0
  • 349
  • 1
  • 6
  • 19

3 Answers3

3

The error message, you are seeing means that you have some variable that contains a nil object instead of the actual object you expect.

While the error message doesn't explicitly reference this, it is likely your @post variable is nil.

Why is it nil? That's near impossible to say given the code here. Please post your PostsController#new action as well.

Jakob S
  • 19,575
  • 3
  • 40
  • 38
  • Thanks, for your answer. I think is better if i put all that staff – Hell0 Sep 03 '13 at 09:11
  • Thanks, for your answer. I think is better if i put all that stuff in other `question`. i'm reading the documentation again, and again i found some other features that i unkown. `Active model only run in Rails 4.0?`, holly b. Thanks anyway. – Hell0 Sep 03 '13 at 09:17
  • That doesn't sound right; I'm fairly sure `ActiveModel` has been a part Rails since the 3.x series. – Jakob S Sep 03 '13 at 09:41
2

The fix is into posts_controller.rb, add next code

def new @post = Post.new end

Good luck

mcorella
  • 21
  • 1
1
// Make sure to use model declaration inside your method to check the error logs

class PostsController < ApplicationController

def new
@post = Post.new
end
Vinod Joshi
  • 7,696
  • 1
  • 50
  • 51