Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7183489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:59:23+00:00 2026-05-28T17:59:23+00:00

My Rails app is generating this error whenever validation aren’t met: undefined method `model_name’

  • 0

My Rails app is generating this error whenever validation aren’t met:

undefined method `model_name' for NilClass:Class

post_controller.rb:

class PostsController < ApplicationController
  def index
    @posts = Post.all
  end

  def show
    @show_post = Post.find(params[:id])
  end

  def new
    @new_post = Post.new
  end



  def create
     @create_post = Post.new(params[:post])

     if @create_post.save
       redirect_to posts_path, :notice => "Your post was save"
       else
     render "new"
     end
  end




  def edit
    @edit_post = Post.find(params[:id])
  end


  def update
    @update_post = Post.find(params[:id])
    if @update_post.update_attributes(params[:post])
      redirect_to posts_path , :notice => "YOUR POST has been update"
    else
       render "edit"

    end

  end

  def destroy
    @destroy_post = Post.find(params[:id])
    @destroy_post.destroy
    redirect_to posts_path, :notice => "You succressfuly delete #{@destroy_post}"

  end
end

index.html.erb:

<h1>My blog</h1>

<% @posts.each do |post|%>
    <h2><%= link_to post.title, post%></h2>
    <p><%= post.content%></p>
    <p>
        <%= link_to "EDIT", edit_post_path(post)%>
        <%= link_to "Delete", post ,:confirm => "Are you sure?" , :method => :delete%>
    </p>
    <hr>
<% end%>

<p>
    <%= link_to "Add new post", new_post_path %>


</p>

post.rb:

class Post < ActiveRecord::Base
   attr_accessible :title, :content

   validates :title, :content, :presence => true
   validates :title, :length => { :minimum => 2}
   validates :title, :uniqueness =>true

end

The error is raised in the following context:

Extracted source (around line #2):

1: <h1>Add new post</h1>
2: <%= form_for @new_post do |form|%>
3:  <p>
4:      <%= form.label :title%><br />
5:      <%= form.text_field :title%>

I don’t think I have a problem with my code? Or the value is just nil and that’s why it comes with this error?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T17:59:25+00:00Added an answer on May 28, 2026 at 5:59 pm

    Stop doing this: @create_post, @new_post, @show_post, @edit_post etc, etc…

    Start doing this: @post.

    The problem is that your object is called @create_post in your create action. If the model is invalid, your create action is calling render "new". The “new” view expects @new_post to be set, which it isn’t. @new_post is nil, and form_for(nil) is raising an error. You should just be calling the variable @post in every method, there is no gain for naming them @create_post or @new_post. It adds meaningless clutter and, in this case, breaks things.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my rails app I use the validation helpers in my active record objects
I have a Rails app that is generating duplicate requests for every request in
My Rails app uses the jquery.post() method to send some data from a clickable
I'm deploying a new Rails app to Heroku. This is the first time I've
Solved, see edit at bottom. In my 3.1 rails app I'm generating a pdf
I want to start out by generating a model to hold this information: rails
Our rails app is designed as a single code base linking to multiple client
My Rails-app has a sign in box with a remember me checkbox. Users who
My Rails app has some pages which are SSL-required and others which are SSL-optional.
A rails app I'm working on features examples of quadratic equations. Obviously, these are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.