如何编辑多态注释?

How to Edit Polymorphic Comments?

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

我试过了:

<%= 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 (Couldn't find Comment with 'id'=2 [WHERE "comments"."commentable_id" = ? AND "comments"."commentable_type" = ?])

我试过要在 def edit 中放什么:

comments_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

您可能会发现 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)将不再是 url 的一部分。您仍然可以通过 @comment.commentable(用于重定向)访问可评论的内容。我遇到的在评论控制器中设置可评论的最干净的解决方案是在 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