简体   繁体   中英

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' %>

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

阅读有关多态关联的信息 ,我认为它们对您的案例非常有帮助。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM