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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:45:48+00:00 2026-05-20T23:45:48+00:00

I realize that this is probably a very basic question, but I have spent

  • 0

I realize that this is probably a very basic question, but I have spent days coming back to it now and for some reason Google just isn’t helping me. (I think part of the problem is that I’m such a beginner I’m not sure what to ask…) I’ve also looked in O’Reilly’s Ruby Cookbook and the Rails API but I am still stuck on this issue. I found some information about polymorphic relationships but it didn’t seem like that was what I needed (although let me know if I was wrong).

I am trying to tweak Michael Hartl’s tutorial to create a blog application with users, articles, and comments (not using scaffolding). I want the comments to belong to both a user and an article.

My main problem is: I can’t figure out how to get the id of the current article into the comments controller.

The relationships for the User class:

class User < ActiveRecord::Base

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

The relationships for the Article class:

class Article < ActiveRecord::Base

belongs_to :user
has_many :comments, :dependent => :destroy

The relationships for the Comment class:

class Comment < ActiveRecord::Base

belongs_to :user
belongs_to :article

This is my CommentsController (the about page renders in the else just to make it obvious to me for the time being):

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

def new
  @comment = Comment.new
end

def create
  @article = Article.find(params[:id])
  @comment = current_user.comments.build(params[:comment])
  @comment.article_id = @article.id
  if @comment.save
    flash[:success] = "Comment created!"
    redirect_to '/contact'
  else
    render '/about'
  end
end

def destroy
end
end

When I log in as a user and try to create a comment on an article, I get “Couldn’t find Article without an ID.” I can’t figure out how to get the id of the current article into the comments controller.

Thanks and let me know if you need me to post more code.

Edit: Here is my _comment_form.html.erb partial which I call at the bottom of my show.html.erb view for the article:

<%= form_for ([@article, @article.comments.build]) do |f| %>
  <div class="field">
    <%= f.text_area :content %>
  </div>
  <div class="actions">
    <%= f.submit "Submit" %>
  </div>
<% end %>

Also here is the show.html.erb for the article:

<heading>
  <h1><%= @article.heading %></h1>
  <p>Posted <%= time_ago_in_words(@article.created_at) %> ago by <%= @article.user.name %></p>
</heading>
<p><%= @article.content %></p>
<footer><p>
  <% unless @article.comments.empty? %>
    <%= @article.comments.count %>
  <% end %> comments</p></footer>
<% unless @article.comments.empty? %>
  <%= render @comments %>
  <%= will_paginate @comments %>
<% end %>
<%= render 'shared/comment_form' %>
  • 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-20T23:45:49+00:00Added an answer on May 20, 2026 at 11:45 pm

    I agree with you, polymorphic is not what you want here. I think your current associations look pretty good.

    I assume that in your routes.rb you have a setup something like this. Correct me if I’m wrong:

    resources :articles do
      resources :comments
    end
    

    But if this is the case, you should change the create action in your CommentsController to use params[:article_id] instead of params[:id]

    @article = Article.find(params[:article_id])
    

    That should fix the problem where it can’t find an Article without an ID

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

Sidebar

Related Questions

I realise that this is a very basic question, but it is one which
I realize that this question is impossible to answer absolutely, but I'm only after
Ok so I realize that this is a pretty vague question, but bear with
I realize that this would be COMPLETELY bad practice in normal situations, but this
I realize that the query this question is looking for won't be enough to
After posting this question and reading that one I realized that it is very
I realize my title probably doesnt explain my situation very well, but I honestly
I've done numerous searches and I realize that I can just download this file
I realize that literally it translates to Java Enterprise Edition. But what I'm asking
I've worked with mysql for some time but have never had to do anything

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.