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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:54:36+00:00 2026-06-01T21:54:36+00:00

I keep getting a undefined method ‘comments’ for nil:NilClass in my create method when

  • 0

I keep getting a undefined method 'comments' for nil:NilClass in my create method when I try to create a new comment(polymorphic relationship). I’ve browsed several other questions regarding this and can’t seem to locate what’s wrong with my form/controller.

Here is my generic partial for comments:

<%= form_for [@commentable, Comment.new] do |f| %>
  <%= f.text_area :content %>
  <%= f.submit "Post" %>
<% end %>

Please note that this is rendered in my traveldeal/show page. The form renders fine. If I change the form_for to pass parameters [@commentable, @comment], I get the error undefined methodmodel_name’ for NilClass:Class`

routes.rb

resources :users 
resources :traveldeals

resources :traveldeals do
  resources :comments
end

resources :users do
  resources :comments
end

The Railscasts has the above written as resources :traveldeals, :has_many => :comments, but I believe this is dated syntax.

comments_controller.rb

class CommentsController < ApplicationController
  before_filter :authenticate, :only => [:create, :destroy]

  def new
    @comment = Comment.new
  end

  def create
    @commentable = find_commentable
    @comment = @commentable.comments.build(params[:comment])
    @comment.user_id = current_user.id

   if @comment.save
      flash[:success] = "Successfully saved comment."  
      redirect_to root_path
    else
      redirect_to current_user
    end
  end

private

  def find_commentable
    params.each do |name, value|
      if name =~ /(.+)_id$/
        return $1.classify.constantize.find(value)
      end
    end
    nil
   end

end

Edit: Added the solution so the code above works for me.

  • 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-01T21:54:37+00:00Added an answer on June 1, 2026 at 9:54 pm

    You have @commentable in your form_for, but where is that variable set? It seems to be set nowhere and I think that is the cause of the error. Also see my answer to your other question.

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

Sidebar

Related Questions

I keep getting: NoMethodError in StudentsController undefined method `id_num=' for #<Array:0x105adcc98> I'm positive there's
I have this code and I keep getting undefined if I test the selectedIndex.
I keep getting an Notice: Undefined index: did error with this query, and I'm
I keep getting a 'List does not exist' error whenever I try and use
I keep getting this error when I try to commit a group of executed
I'm trying to create a nested form but I'm missing something as keep getting
I keep getting compiler errors when I try to access flashVars in an AS3
Keep getting this error after inserting a subdatasheet into a query and trying to
Keep getting the error Arguments are not sufficiently instantiated for the multiplication by addition
I keep getting this : DeprecationWarning: integer argument expected, got float How do I

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.