如何修改多态注释?

时间:2015-06-02 18:44:02

标签: 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 %>

但点击它后我们会收到此错误:

  

ActiveRecord :: RecordNotFound(无法找到评论&#39; id&#39; = 2 [WHERE&#34;评论&#34;。&#34; commentable_id&#34; =?AND&## 34;评论&#34;。&#34; commentable_type&#34; =?])

我在这里玩了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

1 个答案:

答案 0 :(得分: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(用于重定向)访问可评论的内容。我在注释控制器中设置注释的最干净的解决方案是在:commentable_id中查找params的名称,而不是摆弄请求路径。这将导致像这样的控制器:

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