gpt4 book ai didi

ruby-on-rails - 如何向 Feed 添加多态评论?

转载 作者:太空宇宙 更新时间:2023-11-03 16:47:44 25 4
gpt4 key购买 nike

我试图让用户直接在他们的事件提要上添加和查看评论,而不必转到显示页面,这就是我在 railscast 插曲中所做的:http://railscasts.com/episodes/154-polymorphic-association-revised .

我从头开始创建事件提要。

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

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
找不到“id”=

的事件

我试图直接在事件索引中呈现部分评论:

<% @activities.each do |activity| %>
<%= link_to activity.user.name, activity.user %>
<%= render "activities/#{activity.trackable_type.underscore}/#{activity.action}", activity: activity %>

<%= render "comments/comments" %>
<%= render "comments/form" %>
<% end %>

评论/评论 & 评论/表格

<% @comments.each do |comment| %>
<%= User.find(comment.user_id).name %>
<%= simple_format comment.content %>
<% end %>

<%= form_for [@commentable, @comment] do |f| %>
<%= f.text_area :content, rows: 4, class: 'form-control', placeholder: 'Enter Comment' %>
<%= button_tag(type: 'submit', class: "btn") do %>
<span class="glyphicon glyphicon-plus"></span> Comment
<% end %>
<% end %>

activity.rb

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

感谢您的专业知识!

更新

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])

然后在您的 View 中,您可以通过以下方式访问评论:

@activity.comments

关于ruby-on-rails - 如何向 Feed 添加多态评论?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29778140/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com