【问题标题】:How to Edit Polymorphic Comments?如何编辑多态评论?
【发布时间】:2015-08-16 17:14:22
【问题描述】:

由于 cmets 发布在多个位置:习惯、目标等。我使用什么路径来编辑 cmets/_cmets 中的评论?

我试过了:

<%= link_to edit_comment_path(comment) do %> #Or should we use conditionals to target: edit_habit_comment_path, edit_goal_comment_path, etc?
  <%= comment.content %>
<% end %>

但是在点击它时我们会得到这个错误:

ActiveRecord::RecordNotFound(找不到 'id'=2 [WHERE "cmets"."commentable_id" = ? AND "cmets"."commentable_type" = ?] 的评论)

我在这里玩弄了def edit 的内容:

cmets_controller

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
    @habit = Habit.find(params[:id])
    @goal = Goal.find(params[:id])
    @commentable = @habit
    @comments = @commentable.comments
    @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)
    end

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

我密切关注本教程:http://railscasts.com/episodes/154-polymorphic-association-revised

如果您需要进一步的解释或代码来帮助我,请告诉我:)

更新

我为路线尝试了很多替代方案:

Rails.application.routes.draw do

  get 'notes/index'

  get 'notes/new'

  get 'notifications/index'

  get 'auth/:provider/callback', to: 'sessions#facebook'
  get 'auth/failure', to: redirect('/')
  get 'signout', to: 'sessions#destroy', as: 'signout'

  get 'password_resets/new'

  get 'password_resets/edit'

  resources :users do
    resources :comments
  end

  shallow do
    resources :habits do
      resources :comments
    end
    resources :goals do
      resources :comments
    end
  end


  resources :notes

  resources :habits do
    resources :notes
    resources :notifications
    resources :comments do
      resources :likes
    end
    resources :likes
    member do
      post :like
      post :notifications
    end
    resources :levels do
      # we'll use this route to increment and decrement the missed days
      resources :days_missed, only: [:create, :destroy]
    end
  end

  resources :goals do
    resources :notes
    resources :comments
    member do
      post :like
    end
  end

  resources :valuations do
    resources :notes
    resources :comments
    resources :notifications
    member do
      post :like
      post :notifications
    end
  end

  resources :quantifieds do
    resources :notes
    resources :comments
    member do
      post :like
    end
  end

  resources :results

  resources :users

  resources :account_activations, only: [:edit]

  resources :activities do
    resources :valuations
    resources :comments
    resources :notifications
    member do
      post :like
      post :notifications
    end
  end

  resources :comments do
    resources :comments
    member do
      post :like
    end
  end

  resources :password_resets,     only: [:new, :create, :edit, :update]

  resources :relationships,       only: [:create, :destroy]

  get 'tags/:tag', to: 'pages#home', as: :tag

  resources :users do
    member do
      get :following, :followers
    end
  end

  get    'about'   => 'pages#about'
  get    'signup'  => 'users#new'
  get    'login'   => 'sessions#new'
  post   'login'   => 'sessions#create'
  delete 'logout'  => 'sessions#destroy'

  root 'pages#home'
end

【问题讨论】:

    标签: ruby-on-rails ruby model-view-controller comments polymorphic-associations


    【解决方案1】:

    您可能会发现shallow nested routes 很有用。基本上,你写

    shallow do
      resources :goals do
        resources :comments
      end
    
      # ...
    end
    

    相当于

    resources :goals do
      resources :comments, only: [:index, :new, :create]
    end
    resources :comments, only: [:show, :edit, :update, :destroy]
    
    # ...
    

    这意味着,只有那些绝对需要嵌套的动作才会真正被嵌套。如果您只想显示、编辑、更新或销毁评论,它将像任何其他非嵌套资源一样工作,您将能够简单地使用 edit_comment_path

    您将需要相应地更改您的控制器,因为commentable_id(即goal_id)将不再是网址的一部分。您仍然可以通过@comment.commentable(用于重定向)访问可评论。我遇到的在 cmets 控制器中设置可注释的最干净的解决方案是在 params 中查找 :commentable_id 的名称,而不是摆弄请求路径。这将导致这样的控制器:

    class CommentsController < ApplicationController
      before_action :set_commentable, only: [:index, :new, :create]
      before_action :set_comment, only: [:edit, :update, :destroy, :like]
    
      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
      end
    
      def update
        if @comment.update_attributes(comment_params)
          redirect_to @comment.commentable, notice: "Comment was updated."
        else
          render :edit
        end
      end
    
      def destroy
        @comment.destroy
        redirect_to @comment.commentable, notice: "Comment destroyed."
      end
    
      def like
        @comment_like = current_user.comment_likes.build(comment: @comment)
        if @comment_like.save
          @comment.increment!(:likes)
          flash[:success] = 'Thanks for liking!'
        else
          flash[:error] = 'Too many likes'
        end  
        redirect_to(:back)
      end
    
      private
    
      def set_commentable
        @commentable = find_commentable
      end
    
      # add more commentable models here
      def find_commentable
        if params[:goal_id]
          Goal.find(params[:goal_id])
        elsif params[:habit_id]
          Habit.find(params[:habit_id])
        else
          fail 'Unsupported commentable'
        end
      end
    
      def set_comment
        @comment = current_user.comments.find(params[:id])
      end
    
      def comment_params
        params[:comment][:user_id] = current_user.id
        params.require(:comment).permit(:content, :commentable, :user_id, :like)
      end
    end
    

    【讨论】:

    • 我已经更正了您控制器中的一些小问题,您可能会从仔细检查每个操作中学到一些东西。抱歉,如果我在那里留下了一些错误,我现在没有时间真正测试这个。请告诉我,我会尽力纠正它们。
    • 非常感谢您富有洞察力的回答 :) 我现在花了一些时间尝试以我的初学者心态来理解它。使用您的控制器代码和我更新的路线(请参阅问题更新),我遇到了与问题中相同的令人讨厌的错误,其中行指向:@comment = current_user.comments.find(params[:id]),当单击link_to edit_comment_path(comment)
    • 嗯,错误表明没有满足查询的注释。然而,可评论的 id 和类型应该已经从错误中消失了。您是否确定确实存在具有请求 ID 的评论?
    • 我重置了数据库 Patrick 并发表了一条评论然后点击它:ActiveRecord::RecordNotFound in CommentsController#edit Couldn't find Comment with 'id'=1 [WHERE "comments"."commentable_id" = ? AND "comments"."commentable_type" = ?] 指向 @comment = current_user.comments.find(params[:id]) 也许我必须更改 _form 或 _cmets 迭代?
    • 太好了,很高兴我能帮上忙!
    猜你喜欢
    • 2015-02-10
    • 1970-01-01
    • 2022-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多