Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy Ruby on Rails question -- how to attach comments to both users and articles?

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' %>
like image 431
Kelly Avatar asked Mar 29 '11 03:03

Kelly


People also ask

How do you add comments in Ruby on Rails?

The Ruby single-line comment begins with the # character and ends at the end of the line. Any characters from the # character to the end of the line are completely ignored by the Ruby interpreter. The # character doesn't necessarily have to occur at the beginning of the line; it can occur anywhere.

Is Ruby on Rails still relevant 2022?

Ruby's and Ruby on Rails' Overall Popularity Although way behind main contenders, such as PHP or Python, Ruby still makes the cut for the 20 most popular programming languages list in 2022. The 2022 edition of Stack Overflow Annual Developer Survey also places RoR in a similar spot.

What is better Ruby on Rails or Javascript?

Performance – Ruby on Rails has medium level performance as it doesn't support asynchronous programming. It also has a high CPU processing time, while Javascript has greater performance with asynchronous support and event driven single threaded architecture.

How do I create a controller in Rails?

To generate a controller and optionally its actions, do the following: Press Ctrl twice and start typing rails g controller. Select rails g controller and press Enter . In the invoked Add New Controller dialog, specify the controller name.


1 Answers

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

like image 192
DanneManne Avatar answered Sep 19 '22 03:09

DanneManne