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

  • SEARCH
  • Home
  • 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 6675091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:48:39+00:00 2026-05-26T03:48:39+00:00

I have created a very simple Rails 3.1 app with basic stuffs like following:

  • 0

I have created a very simple Rails 3.1 app with basic stuffs like following:

My PostsController.rb has new, index, create methods:

def index
    @posts = Post.all

    render :index
end
def new
   @post = Post.new
   render :new
end

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

    if @post.save
      render :index

    else
      render :new

    end

  end

The new.html.erb form:

<%= form_for @post do |f| %>
  <%= f.error_messages%>
  <p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </p>
  <p>
    <%= f.label :address %><br />
    <%= f.text_field :address, :size=>60 %>
  </p>

  <p>
    <%= f.submit 'Create' %>
  </p>
<% end %>

the index view (index.html.erb):

<table>
<% @posts.each do |post| %>
  <tr>
    ...
  </tr>
<% end %>
</table>

my routes.rb has defined:

resources :posts
root :to => 'post#new'

With all above, when the new form is submited, the create method in PostController will be invoked, then inside the create method, if the new post is saved successfully, it will render :index,that’s to render the index.html.erb view, things are fine at this point. but I got a error now in index.html.erb view:

It complains that the @posts in <% @posts.each do |post| %> is nil, why???

————-I also tried————————-

I tried to use redirect_to posts_path in create method, then I got undefined method `posts_path_url’ error

I also tried to use redirect_to :index in create method, then I got undefined method `index_url’ 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-26T03:48:40+00:00Added an answer on May 26, 2026 at 3:48 am

    The error happens because @posts variable is not set in your Post#create method as it is in Post#index. You have to remember that rendering a view doesn’t call appropriate controller method.

    You want to use redirect_to instead of render:

    def create
        @post = Post.new(params[:post])
    
        if @post.save
            redirect_to :posts
        else
            render :new
        end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simple table with two columns, but has 4.5M rows. CREATE
This should be a very simple rails question. I have a variable like the
I have created a very simple Tree implementation, and I would like to be
I have a very basic rails app. I am playing around with validation. Controller
I have created a very simple sharepoint timer job. All i want it to
I am using SQL Server 2008 Enterprise. I have created a very simple test
I'm trying to create a very simple REST server. I just have a test
i have very simple problem. I need to create model, that represent element of
I've got a very simple Rails 3 app with a User model run by
I have created a very simple application where I am trying to insert a

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.