将注释添加到多个模型

时间:2016-02-07 08:18:04

标签: ruby-on-rails comments models

我尝试为我的主题模型添加评论,就像您可以在我的应用上添加评论一样。我目前不得不赞成评论_comment.html.erb_form.html.erb

_comment:

<%= content_tag :div, class: 'media', id: "comment-#{comment.id}" do %>
<div class= "media">
  <div class= "media-body">
    <small>
      <%= comment.user.name %> commented <%= time_ago_in_words(comment.created_at) %> ago
      <% if user_is_authorized_for_comment?(comment) %>
      | <%= link_to "Delete", [comment.post, comment], method: :delete %>
      <% end %>
    </small>
    <p> <%= comment.body %></p>
  </div>
</div>
<% end %>

_form:

<h4>Add a comment</h4>
<%= form_for [post, comment] do |f| %>
  <div class="form-group">
    <%= f.label :body, class: 'sr-only' %>
    <%= f.text_field :body, class: 'form-control', placeholder: "Enter a new comment" %>
  </div>
  <%= f.submit "Submit Comment", class: 'btn btn-default pull-right' %>
<% end %>

我的主题是:

#DISPLAY Topic comments here
<h3> Comments</h3>
<%= render @topic.comments %>
</div>
<% if current_user %>
<%= render 'comments/form', comment: Comment.new, post: @post %>
<% end %>
#------

评论控制器:

  def create
    @post = Post.find(params[:post_id])
    comment = @post.comments.new(comment_params)
    comment.user = current_user

    if comment.save
      flash[:notice] = "Comment saved successfully."
      redirect_to [@post.topic, @post]
    else
      flash[:alert] = "Comment failed to save."
      redirect_to [@post.topic, @post]
    end
  end

  def destroy
    @post = Post.find(params[:post_id])
    comment = @post.comments.find(params[:id])

    if comment.destroy
      flash[:notice] = "Comment was deleted"
      redirect_to [@post.topic, @post]
    end
  end

我更新了主题评论的路线:

  resources :topics do
    resources :posts, except: [:index]
    resources :comments, only: [:create, :destroy]
  end

我的问题是,我是否需要创建一个单独的部分以向主题添加评论,或者我可以更新我的_comment部分以用于帖子和主题评论。我怎么能做到这一点?

2 个答案:

答案 0 :(得分:1)

<强>模型

您需要Comment模型polymorphic association

#app/models/comment.rb
class Comment < ActiveRecord::Base
   belongs_to :commentable, polymorphic: true
end

#app/models/topic.rb
class Topic < ActiveRecord::Base
   has_many :comments, as: :commentable
end

#app/models/post.rb
class Post < ActiveRecord::Base
   has_many :comments, as: :commentable
end

<强>控制器

这将允许您保存各种型号的评论,控制器/流程将成为次要的:

#config/routes.rb
resources :topics, :posts do
   resources :comments, only: [:create, :destroy] #-> url.com/topics/:topic_id/comments
end

#app/controllers/comments_controller.rb
class CommentsController < ApplicationController
  def create
    id = params[:post_id] || params[:topic_id]
    if params[:post_id]
      @parent = Post.find id
    elsif params[:topic_id]
      @parent = Topic.find id
    end

    @comment = @parent.comments.find params[:id]
    @comment.save
  end

  def destroy
    @parent  = params[:post_id] || params[:topic_id]
    @comment = @parent.comments.new comment_params
    @comment.destroy
  end

  private

  def comment_params
    params.require(:comment).permit(:x, :y)
  end 
end 

由于您已将数据传递给comments控制器,因此您只需评估您正在使用的@parent

<强>视图

对于您的观看次数,您需要将locals传递给_form部分:

#app/views/posts/show.html.erb
<%= render "comments/form", locals: {parent: @post} %>

#app/views/comments/_form.html.erb
<%= form_for [parent, parent.comments.new] do |f| %>

答案 1 :(得分:1)

我也有这个路障,这就是我想到的那个过去了。我必须首先给@Richard Peck掌声让我的车轮转动,所以谢谢你:)。

<强>模型

是否实现了adb shell pm install -r app.apk 。坚持polymorphic associationhas_many,仅此而已

<强>局部模板

_comment.html.erb

设置删除部分以接受“父”作为本地

belongs_to

_form.html.erb

与_comment.html.erb相同,见上文

<div class="media">
  <div class="media-body">
    <small>
      <%= comment.user.name %>
      commented
      <%= time_ago_in_words(comment.created_at) %>
      ago
      <% if user_is_authorized_for_comment_via_post?(comment) %>
      |
      <%= link_to "Delete", [parent, comment], method: :delete %>
      <% end %>
    </small>
    <p>
      <%= comment.body %>
    </p>
  </div>
</div>

设置CommentController

<h4>Add a comment</h4>


  <%= form_for [parent, comment] do |f| %>
    <div class="form-group">
      <%= f.label :body, class: 'sr-only' %>
      <%= f.text_field :body, class: 'form-control', placeholder: "Enter a new comment" %>
    </div>

  <%= f.submit "Submit Comment", class: 'btn btn-default pull-right' %>

<% end %>

传递主题/节目和帖子/节目的评论

... def create # upon clicking on create, determine what param id is passed if params[:post_id] # if it is a post id, set instance of post id as @parent @parent = Post.find(params[:post_id]) elsif params[:topic_id] # if it is a topic id, set instance of topic id as @parent @parent = Topic.find(params[:topic_id]) end # create instance as @comment. Build/create # comment belonging to @parent (Topic or Post) @comment = @parent.comments.build(comment_params) # The comment must be associated to the current user. # A comment must have a user, and value of user within instance of @comment # is currently nil. Set user id as current user @comment.user = current_user # save comment to database if @comment.save # direction of save through if and elsif # Redirection depends on the comment's parent. # .is_a? method determines if it is of a certain class. Here, is @parent # of class Post? Is @parents is the same parent id passed through params? if @parent.is_a?(Post) # template error with this included: (== params[:post_id]) flash[:notice] = 'Comment saved successfully' redirect_to [@parent.topic, @parent] # if not part of the class Post, is it a Topic? If so, save here and # redirect to the topic after save elsif @parent.is_a?(Topic) flash[:notice] = 'Comment saved successfully' redirect_to @parent end end end def destroy comment = Comment.find(params[:id]) # @topic = Topic.find(params[:topic_id]) # topic_comment = @topic.comments.find(params[:id]) # @post = Post.find(params[:post_id]) # post_comment = @post.comments.find(params[:id]) if comment.destroy flash[:notice] = 'Comment was deleted' redirect_to :back else flash[:alert] = "Comment counld't be deleted. Try again" redirect_to :back end end ...

注意:注意topic/show如何从此处传递到控制器

locals

... <div class="row"> <%= render 'comments/form', comment: Comment.new, parent: @topic %> </div> <% @topic.comments.each do |comment| %> <%= render partial: 'comments/comment', locals: { parent: @topic, comment: comment } %> <% end %> ...

post/show

希望这会有所帮助。