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 8546771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:06:43+00:00 2026-06-11T13:06:43+00:00

I am learning ruby on rails with the Getting Started tutorial on the official

  • 0

I am learning ruby on rails with the Getting Started tutorial on the official site here. http://guides.rubyonrails.org/getting_started.html

The only thing I have changed is the post url mapping to point to /post/friendly-url instead of /post/id

Everything has gone smooth until I try to add a comment to a post, I receive the following error.

NoMethodError in CommentsController#create

undefined method `comments' for nil:NilClass
app/controllers/comments_controller.rb:7:in `create'

Here is my code.

/app/controllers/comments_controller.rb

class CommentsController < ApplicationController
    def create
        @post = Post.find_by_friendly(params[:id])
        @comment = @post.comments.create(params[:comment])
        redirect_to post_path(@post)
    end

    def destroy
        @post = Post.find_by_friendly(params[:id])
        @comment = @post.comments.find(params[:id])
        @comment.destroy
        redirect_to post_path(@post)
    end
end

/app/models/comment.rb

class Comment < ActiveRecord::Base
    belongs_to :post

    attr_accessible :body, :commenter

    validates :body,  :presence => true
    validates :commenter,  :presence => true
end

/app/views/comments/_form.html.erb

<%= form_for([@post, @post.comments.build]) do |f| %>

<%= f.label :commenter, "Your Name:" %>
    <%= f.text_field :commenter, :placeholder => "Your Name..." %>
<span class="help-block">What would you like to be called.</span><br/>

<%= f.label :body, "Your Comment:" %>
    <%= f.text_area :body, :size => "60x12", :placeholder => "Your Comment..." %>
<span class="help-block">What's on your mind?</span><br/>

    <%= f.submit %>

<% end %>

/app/views/posts/_form.html.erb

<% @post.tags.build %>
<%= form_for(@post) do |post_form| %>
<legend>Post Form</legend>
<% if @post.errors.any? %>
    <div id="error_explanation">
        <h2 class="text-error"><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
        <ul>
            <% @post.errors.full_messages.each do |msg| %>
                <li class="text-error"><%= msg %></li>
            <% end %>
        </ul>
    </div>
<% end %>

<%= post_form.label :name, "Post Name:" %>
    <%= post_form.text_field :name, :placeholder => "Post Name..." %>
<span class="help-block">The title of the article.</span><br/>

<%= post_form.label :friendly, "Friendly URL:" %>
    <%= post_form.text_field :friendly, :placeholder => "Friendly URL..." %>
<span class="help-block">SEO friendly URL displayed as /posts/post-name.</span><br/>

<%= post_form.label :content, "Post Content:" %>
<%= post_form.text_area :content, :size => "60x12", :placeholder => "Main Content..." %>
<span class="help-block">HTML enabled article content.</span><br/>

<%= post_form.label :excerpt, "Post Excerpt:" %>
    <%= post_form.text_area :excerpt, :placeholder => "Post Excerpt..." %>
<span class="help-block">Description of post for index page. No HTML.</span><br/>

<h2>Tags</h2>
<%= render :partial => 'tags/form',
    :locals => {:form => post_form} %><br/>

    <%= post_form.submit %>

<% end %>

/config/routes.rb

Nullpulse::Application.routes.draw do
    resources :posts do
        resources :comments
    end

    root :to => "home#index"
end

/apps/models/post.rb

class Post < ActiveRecord::Base
    attr_accessible :content, :friendly, :name, :excerpt, :tags_attributes

    validates :name,  :presence => true
    validates :content, :presence => true
    validates :friendly, :presence => true
    validates :excerpt, :presence => true
    validates_format_of :friendly, :with => /^[^ ]+$/

    has_many :comments, :dependent => :destroy
    has_many :tags

    accepts_nested_attributes_for :tags, :allow_destroy => :true,
        :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }

    def to_param
        friendly
    end
end

Sorry if that is too much information, also please let me know if I am missing anything. I have been trying everything and I can’t find the issue. Thanks in advance.

  • 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-06-11T13:06:44+00:00Added an answer on June 11, 2026 at 1:06 pm

    The issue is that your @post in CommentController#create is nil. Which means, either your params[:id] that you’re providing is incorrect, or it isn’t being found in your database. I’d suggest checking the logs to see what params[:id] contains, and see if that matches what find_by_friendly is expecting.

    If the params[:id] looks correct, I’d use the rails console to try the Posts.find_by_friendly and pass in some values to see if that works for you.

    If the params[:id] value doesn’t look correct, then your form_for() call in comments/_form.html.erb is probably wrong, take a look at the docs for the friendly plugin to see how to make the correct call.

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

Sidebar

Related Questions

I started learning Ruby from ( http://guides.rubyonrails.org/getting_started.html ) and successfully installed ruby and created
Quick question: I have been learning ruby on rails and http://guides.rubyonrails.org/index.html is really good
I'm learning ruby on rails by following the tutorials in http://ruby.railstutorial.org/ . I'm getting
I am learning ruby from this guide http://ruby.railstutorial.org/ruby-on-rails-tutorial-book#sec:deploying , and I am trying to
Learning Ruby and Rails. In following the getting started guide if I invoke rails
I am learning ruby on rails. I started by importing some data into a
I'm a PHP developer learning Ruby on Rails by reading Michael Hartl's tutorial .
I started learning Ruby on Rails and found myself confounded by the syntax, so
I've just started learning Ruby and Ruby on Rails and came across validation code
I recently started learning Ruby and Ruby on Rails, and have watched a plethora

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.