Rails 4 - 多态关联 - 嵌套属性

Rails 4 - Polymorphic Associations - nested attributes

我正在尝试使用 Rails 4.

制作一个应用程序

我用的是简单的表格。

我正在尝试按照本教程进行操作,以便我的多态评论模型可用于向文章添加评论。 https://gorails.com/episodes/comments-with-polymorphic-associations?autoplay=1

我有文章和评论的模型如下:

article.rb

has_many :comments, as: :commentable
      accepts_nested_attributes_for :comments

comment.rb

    belongs_to :user
  belongs_to :commentable, :polymorphic => true

已按照视频教程中所示设置评论控制器:

comments_controller.rb

Article/comments_controller.rb

文章管理员有:

def new
    @article = Article.new
    @article.comments.build
  end


def article_params
      params[:article].permit(:user_id, :body, :title, :image, :tag_list,
        comment_attributes: [:opinion])
    end

文章展示页有:

<div class="col-xs-12">
  <%= render :partial => 'comments/form', locals: {commentable: @article}  %>
</div>

评论部分有:

 <%= simple_form_for [commentable, Comment.new] do |f| %>
                 <%= f.error_notification %>

          <div class="form-inputs">
           <%= f.input :opinion, as: :text, :label => "Add your thoughts", :input_html => {:rows => 4} %>
          </div>

          <div class="form-actions">
            <%= f.button :submit, "Submit", :class => 'formsubmit' %>
          </div>
        <% end %>

路线是:

resources :articles do
    collection do 
      get 'search' 
    end
    resources :comments, module: :articles
  end

当我保存所有这些并尝试呈现文章显示页面时,出现此错误:

#

的未定义方法“新”

错误指向评论控制器创建操作:

def create
    @comment = @commentable.new(comment_params)
    @comment.user = current_user


    respond_to do |format|
      if @comment.save
        format.html { redirect_to @commentable }
        format.json { render :show, status: :created, location: @comment }
      else
        format.html { render :new }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end

我不知道是什么问题。我不明白为什么这不起作用或此错误消息的含义。我想知道是不是因为评论既属于用户又属于可评论。

事实上,当我推送它并尝试在生产模式下查看它时,我遇到了失败并且 heroku 日志显示了这个错误:

Exiting
2016-01-02T02:27:59.274318+00:00 app[web.1]: /app/app/controllers/Articles/comments_controller.rb:1:in `<top (required)>': uninitialized constant Articles (NameError)

整个Article/comments控制器有:

class Articles::CommentsController < CommentsController

    before_action :set_commentable#, only: [:show, :edit, :update, :destroy]


  private
   # Use callbacks to share common setup or constraints between actions.
    def set_commentable
      @commentable = Article.find(params[:article_id])
    end
end

所以这现在适用于新文章,但仅适用于 1 条评论。如果我尝试向单篇文章添加第二条评论,我会收到此错误:

PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "index_comments_on_commentable_type_and_commentable_id" DETAIL: Key (commentable_type, commentable_id)=(Article, 4) already exists. : INSERT INTO "comments" ("opinion", "user_id", "commentable_id", "commentable_type", "created_at", "updated_at") VALUES (, , , , , ) RETURNING "id"

尝试:

def create
  @comment = Comment.new(comment_params)
  @comment.user = current_user
  @comment.commentable = @commentable

  respond_to do |format|
    if @comment.save
      format.html { redirect_to @comment }
      format.json { render :show, status: :created, location: @comment }
    else
      format.html { render :new }
      format.json { render json: @comment.errors, status: :unprocessable_entity }
    end
  end
end

很多问题。

它应该是这样的:

--

#config/routes.rb
resources :articles do
  get :search, on: :collection
  resources :comments, module: :articles #-> url.com/articles/:article_id/comments
end

#app/controllers/articles/comments_controller.rb #-> this is the heroku error
class Articles::CommentsController < ApplicationController
    before_action :set_article
    respond_to :js, :json, :html

    def create
       @comment = @article.comments.new comment_params
       respond_with @comment
    end

    private

    # Use callbacks to share common setup or constraints between actions.
    def set_article
       @article = Article.find params[:article_id]
    end

    def comment_params
       params.require(:comment).permit(:opinion).merge(user: current_user)
    end
end

您应该将评论创建为它们自己的实体(而不是作为文章的嵌套属性)。您应该使用以下方法执行此操作:

#app/views/articles/show.html.erb
<%= content_tag :div, render(partial: 'comments/form', locals: { article: @article }), class: "col-xs-12" %>

#app/views/comments/_form.html.erb
<%= simple_form_for [article, article.comments.new] do |f| %>
   <%= f.input :opinion, as: :text, label: "Add your thoughts", input_html: {rows: 4} %>
   <%= f.submit %>
<% end %>

这应该在被调用的 Article 的背面创建一个独立的注释。