如何添加多态注释到Feed?

我正在尝试允许用户直接在他们的活动源上添加和查看评论,而不必去显示页面,这是我在这个railscast剧集中所做的: http ://railscasts.com/episodes/ 154-多态关联 – 修订 。

我从头开始创建活动源。

以下是我所做的尝试之一。 我将逻辑直接添加到索引操作中:

class ActivitiesController < ApplicationController def index @activities = Activity.order("created_at desc").where(current_user.following_ids) @activity = Activity.find(params[:id]) @commentable = @activity @comments = @commentable.comments @comment = Comment.new end end 

它告诉我: ActiveRecord::RecordNotFound in ActivitiesController#index Couldn't find Activity with 'id'=

我试图直接在活动索引中呈现注释部分:

       

评论/意见评论/表格

         Comment   

activity.rb

 class Activity < ActiveRecord::Base belongs_to :user has_many :comments, as: :commentable belongs_to :trackable, polymorphic: true end 

感谢您的专业知识!

UPDATE

comments_controller.rb

 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) end def comment_params params[:comment][:user_id] = current_user.id params.require(:comment).permit(:content, :commentable, :user_id, :like) end end 

模式

 create_table "activities", force: true do |t| t.integer "user_id" t.string "action" t.integer "trackable_id" t.string "trackable_type" t.datetime "created_at", null: false t.datetime "updated_at", null: false end add_index "activities", ["trackable_id"], name: "index_activities_on_trackable_id" add_index "activities", ["user_id"], name: "index_activities_on_user_id" 
  activities GET /activities(.:format) activities#index POST /activities(.:format) activities#create new_activity GET /activities/new(.:format) activities#new edit_activity GET /activities/:id/edit(.:format) activities#edit activity GET /activities/:id(.:format) activities#show PATCH /activities/:id(.:format) activities#update PUT /activities/:id(.:format) activities#update DELETE /activities/:id(.:format) activities#destroy 

您可以执行以下操作:

 # activites/index.html.erb <% @activities.each do |activity| %> # etc. <%= render "comments/comments", comments: activity.comments %> <%= render "comments/form" %> <% end %> # comments/_comments.html.erb <% comments.each do |comment| %> # your code to display each comment <% end %> 

你可以看到我向render方法传递了一个参数:

 render "comments/comments", comments: activity.comments 

它会给_comments局部变量comments ,它将等于activity.comments


您甚至可以使用部分form执行几乎相同的操作:

 # activites/index.html.erb <%= render "comments/form", new_comment: Comment.new(commentable_id: activity.id, commentable_type: activity.class.model_name) %> # comments/_form.html.erb <% form_for new_comment do |f| %> 

此外,Ruby on Rails中的一个好习惯是不要创建多个实例变量@like_this_one 。 保持最低限度,因为它可能会令人困惑。 我要补充一点,你的变量命名也很重要。

下列:

 @activity = Activity.find(params[:id]) @commentable = @activity @comments = @commentable.comments 

可以重构为:

 @activity = Activity.find(params[:id]) 

然后在您的视图中,您可以通过执行以下操作来访问评论:

 @activity.comments