如何在Feed上提交多态评论? [错误] [英] How to Submit Polymorphic Comments on Feed? [Error]

查看:65
本文介绍了如何在Feed上提交多态评论? [错误]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用户单击 [+评论] 按钮

他面对着这只邪恶的野兽:

he is confronted with this evil beast:

ActiveRecord::RecordNotFound in CommentsController#create
Couldn't find Comment with 'id'=
Line: @commentable = resource.singularize.classify.constantize.find(id)

活动/索引

<%= link_to activity.user.name, activity.user %>
<%= render "activities/#{activity.trackable_type.underscore}/#{activity.action}", activity: activity %>
<%= render "comments/comments", comments: activity.comments %>
<%= render "comments/form", new_comment: Comment.new(commentable_id: activity.id, commentable_type: activity.class.model_name) %>

评论/_表单

<%= form_for new_comment do |f| %>
  <%= f.text_area :content, rows: 4, class: 'form-control', placeholder: 'Enter Comment' %>
  <%= button_tag(type: 'submit', class: "btn") do %>
    <span class="glyphicon glyphicon-plus"></span> Comment
  <% end %>
<% end %>

activities_controller

def index
  @activities = Activity.order("created_at desc").where(user_id: current_user.following_ids)
  @commentable = @activity
  @comment = Comment.new
end

可以在这里找到错误:

class CommentsController < ApplicationController
  before_action :load_commentable
  before_action :set_comment, only: [:show, :edit, :update, :destroy, :like]
  before_action :logged_in_user, only: [:create, :destroy]

    def index
        @comments = @commentable.comments
    end

    def new
        @comment = @commentable.comments.new
    end

    def create
        @comment = @commentable.comments.new(comment_params)
        if @comment.save
            redirect_to @commentable, notice: "comment created."
        else
            render :new
        end
    end

    def edit
        @comment = current_user.comments.find(params[:id])
    end

    def update
        @comment = current_user.comments.find(params[:id])
        if @comment.update_attributes(comment_params)
            redirect_to @commentable, notice: "Comment was updated."
        else
            render :edit
        end
    end

    def destroy
        @comment = current_user.comments.find(params[:id])
        @comment.destroy
        redirect_to @commentable, notice: "comment destroyed."
    end

  def like
    @comment = Comment.find(params[:id])
    @comment_like = current_user.comment_likes.build(comment: @comment)
    if @comment_like.save
            @comment.increment!(:likes)
        flash[:success] = 'Thanks for liking!'
    else
        flash[:error] = 'Two many likes'
      end  
        redirect_to(:back)
  end

private
  def set_comment
    @comment = Comment.find(params[:id])
  end

    def load_commentable
        resource, id = request.path.split('/')[1, 2]
        @commentable = resource.singularize.classify.constantize.find(id) #Here it is!
    end

    def comment_params
        params[:comment][:user_id] = current_user.id
        params.require(:comment).permit(:content, :commentable, :user_id, :like)
    end
end

错误来自此处的答案:如何将多态注释添加到Feed?

The error came about from the answer here: How to Add Polymorphic Comments to Feed?

development.log

Started POST "/comments" for ::1 at 2015-04-23 20:12:14 -0400
Processing by CommentsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"hU1lxg2BMqSyBo8j2SGiEB4wZ3ez5kz/E64mp6ssbwBnh+DddyTtNQxY+IYCluHHvs2wIBxrtD5hQVA5sGtXBg==", "comment"=>{"content"=>"test"}, "button"=>""}
  [1m[35mUser Load (0.2ms)[0m  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 2]]
  [1m[36mHabit Load (0.1ms)[0m  [1mSELECT "habits".* FROM "habits" WHERE "habits"."user_id" = ?[0m  [["user_id", 2]]
  [1m[35mHabit Load (2.5ms)[0m  SELECT "habits".* FROM "habits"
  [1m[36mActsAsTaggableOn::Tag Load (0.3ms)[0m  [1mSELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('ingrain'))[0m
  [1m[35mComment Load (0.3ms)[0m  SELECT  "comments".* FROM "comments" WHERE "comments"."id" = ? LIMIT 1  [["id", nil]]
Completed 404 Not Found in 16ms

ActiveRecord::RecordNotFound (Couldn't find Comment with 'id'=):
  app/controllers/comments_controller.rb:61:in `load_commentable'

非常感谢您!

推荐答案

问题在于,要使其正常工作,似乎已设置注释为嵌套资源,无论您使用什么在您的路线文件中重新评论.

The problem is, for this to work, it looks like it is setup for comments to be a nested resource of whatever you're commenting on in your routes file.

因此,如果您想对活动发表评论,您将:

So, if you want comments on activities, you'd have:

resources :activites do
  resources :comments
end

这样,当#load_commentable方法分离出请求路径时,它将从前两个段中获得注释和ID.

This way, when the #load_commentable method picks apart the request path, it'll get the commentable and id from the first two segments.

相反,它看起来就像您正在尝试将注释用作顶级资源.

It looks, instead, like you're trying to use comments as a top level resource.

更新: 调用部分代码时,只需传递表单应使用的url帮助器即可.像这样:

UPDATE: When you call your partial, simply pass along the url helper that the form should use. Like this:

<%= render "comments/form", new_comment: Comment.new(commentable_id: activity.id, commentable_type: activity.class.model_name), create_url: :activity_comments_path %>

然后,在部分中,只需调用该帮助程序并将结果作为url选项传递-像这样:

Then, over in the partial, simply invoke that helper and pass the result as the url option - like this:

<%= form_for new_comment, url: send(create_url, new_comment.commentable)

这篇关于如何在Feed上提交多态评论? [错误]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆