【问题标题】:How to add user name to comment?如何在评论中添加用户名?
【发布时间】:2015-03-30 18:30:52
【问题描述】:

当用户创建评论时。我们怎样才能把他的名字加进去呢?我尝试了 current_user ,但这显然只是陈述当前用户的姓名,而不是发表评论的人的姓名。

在用户模型has_many :comments, as: :commentable 中,在评论模型belongs_to :commentable, polymorphic: true 中,在路由中被resources :users do resources :comments end 分解。

views/cmets/_cmets.html.erb

<div id="comments">
<% @comments.each do |comment| %>
    <div class="comment">
        <%= current_user.name %>
        <%= simple_format comment.content %>
    </div>
<% end %>

cmets 控制器

class CommentsController < ApplicationController
	before_action :load_commentable
  before_action :set_comment, only: [:show, :edit, :update, :destroy]
  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
			@comment.create_activity :create, owner: current_user 
			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
		@comment.create_activity :destroy, owner: current_user
		redirect_to @commentable, notice: "comment destroyed."
	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.require(:comment).permit(:content, :commentable)
	end
end

我通过创建控制器在活动提要中显示各种名称。因为我尝试过但失败了,所以我可以在这里采取哪些原则来使其发挥作用。

class ActivitiesController < ApplicationController
 def index
 @activities = PublicActivity::Activity.order("created_at desc").where(owner_id: current_user.following_ids, owner_type: "User")
 end
end

用户迁移

class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
  t.string   :name
  t.string   :email
  t.string   :provider
  t.string   :uid
  t.string   :oauth_token
  t.datetime :oauth_expires_at

  t.timestamps null: false
end
end
end

cmets 迁移

class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
    t.text :content
    t.belongs_to :commentable, polymorphic: true

  t.timestamps null: false
end
add_index :comments, [:commentable_id, :commentable_type]
end
end

valuations_controller

class ValuationsController < ApplicationController
  before_action :set_valuation, only: [:show, :edit, :update, :destroy]
  before_action :logged_in_user, only: [:create, :destroy]

  def index
    if params[:tag]
      @valuations = Valuation.tagged_with(params[:tag])
    else
      @valuations = Valuation.order('RANDOM()')
    end
  end

  def show
    @valuation = Valuation.find(params[:id])
    @commentable = @valuation
    @comments = @commentable.comments
    @comment = Comment.new
  end

  def new
    @valuation = current_user.valuations.build
    @commentable = @valuation
    @comments = @commentable.comments
    @comment = Comment.new
  end

  def edit
  end

  def create
    @valuation = current_user.valuations.build(valuation_params)
    if @valuation.save
      redirect_to @valuation, notice: 'Value was successfully created'
    else
      @feed_items = []
      render 'pages/home'
  end
end

  def update
    if @valuation.update(valuation_params)
      redirect_to @valuation, notice: 'Value was successfully updated'
    else
      render action: 'edit'
  end
end

  def destroy
    @valuation.destroy
    redirect_to valuations_url
  end

private
    def set_valuation
      @valuation = Valuation.find(params[:id])
    end

    def correct_user
      @valuation = current_user.valuations.find_by(id: params[:id])
      redirect_to valuations_path, notice: "Not authorized to edit this valuation" if @valuation.nil?
    end

    def valuation_params
      params.require(:valuation).permit(:name, :private_submit, :tag_list, :content, :commentable, :comment)
    end
end

感谢您的宝贵时间!

【问题讨论】:

  • 在他们发表评论之前,您如何验证使用情况。您需要收集他的详细信息或通过第三方api进行身份验证并获取信息

标签: ruby-on-rails ruby


【解决方案1】:

假设你有

用户模型:

class User
  has_many :comments
  ...
end

评论模型

class Comment
  belongs_to :user
  ...
end

评论迁移

class CreateComments < ActiveRecord::Migration
  def change
    create_table :comments do |t|
      t.text :content
      t.belongs_to :user, index: true

      t.timestamps null: false
    end
  end
end

你可以通过

获取评论的用户名
@comment = Comment.find(params[:id])
@comment.user.name

编辑:如果您想使用多态关联,请像这样编辑您的模型

class User
  has_many :comments, as: :commentable
  ...
end

class Comment
  belongs_to :commentable, polymorphic: true
  ...
end

编辑:插入评论迁移

class CreateComments < ActiveRecord::Migration
  def change
    create_table :comments do |t|
      t.text :content
      t.references :commentable, polymorphic: true, index: true

      t.timestamps null: false
    end
  end
end

与 ValuationsController 相关的编辑

  1. 您正在寻找创建特定评估评论的用户。 @valuation 持有此评论。因此,您可以使用@valuation.commentable.user.name 获取用户 - 这假设您的 Valuation 设置了与 Comment 模型的正确关联。

  2. 删除@commentable = @valuation@comments = @commentable.comments。它们没有错,但在这种情况下,保持具有相同值的变量没有任何意义。

  3. 在“显示”操作中,您已将 @comment 分配给 Comment.new。删除此行,因为您没有在 show 操作中创建新对象。

  4. 您已经在类顶部的 before_action 中为 @valutation 分配了相同的值。为防止混淆,请在显示操作中删除此附加分配。

未来调试的线索:如果您的代码没有按预期运行,请尝试在 rails 控制台中运行它。我们昨天已经看到,@comment.commentable.user.name 周围的代码是正确的。这告诉我,你的控制器一定有问题。

最终解决方案

&lt;%= User.find(comment.user_id).name %&gt; 添加到_cmets.html.erb

【讨论】:

  • 我已经有def set_comment @comment = Comment.find(params[:id]) end。如果我在视图中使用@comment.user.name,则会收到错误消息:undefined method user for #&lt;Comment:0x007ffe27b78e00&gt;。不过谢谢!
  • 用户和 cmets 的迁移情况如何?
  • 尝试使用owner 而不是user
  • 对不起,我指的不是代码,而是估值的值。嗯,在控制台上你会得到你想要的结果。您究竟想在哪里检索创建用户名?在表演动作中?请使用整个相关控制器更新帖子。
猜你喜欢
  • 2014-07-06
  • 1970-01-01
  • 1970-01-01
  • 2015-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多