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' %>
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With