Ruby on rails 如何编辑多态注释?

Ruby on rails 如何编辑多态注释?,ruby-on-rails,ruby,model-view-controller,comments,polymorphic-associations,Ruby On Rails,Ruby,Model View Controller,Comments,Polymorphic Associations,由于评论被发布在多个位置:习惯、目标等。我使用什么路径编辑评论/_评论中的评论 我试过: <%= 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 %> 我密切关注本教程:

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

我试过:

<%= 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 %>
我密切关注本教程:

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

更新 我为路线尝试了很多选择:

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 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\u comment\u path

您需要相应地更改控制器,因为
可注释的\u id
(即
目标\u id
)将不再是url的一部分。您仍然可以通过
@comment.commentable
(用于重定向)访问commentable。在comments控制器中设置commentable的最干净的解决方案是在
参数中查找
: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
class CommentsController
我已经纠正了控制器中的一些小问题,您可以通过仔细检查每个操作来了解一些东西。抱歉,如果我在这里留下了一些错误,我现在没有时间真正测试这个。只要让我知道,我会尽我最大的努力纠正它们。非常感谢你富有洞察力的回答:)我花了一些时间试图用我的初学者心态来理解它。使用您的控制器代码和我更新的路线(请参阅问题更新),我遇到了与问题中相同的令人讨厌的错误,行指向:
@comment=current\u user.comments.find(params[:id])
当单击
链接以编辑注释路径(comment)时
错误表明没有满足查询要求的注释。但是,可注释id和类型应该已从错误中消失。您是否确保具有请求id的注释确实存在?我重置了数据库并对其进行了一次注释,然后单击:
ActiveRecord::RecordNotFound in CommentsController#edit找不到id=1的注释[其中“comments”。“commentable_id=”和“comments”。“commentable_type=?]
指向
@comment=current_user.comments.find(params[:id])
也许我必须更改_表单或_comments迭代?太好了,很高兴我能帮上忙!
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