【问题标题】:gem devise how to add comment created user email?gem 设计如何添加评论创建的用户电子邮件?
【发布时间】:2020-11-03 06:41:20
【问题描述】:

我正在使用 gem 设计来创建用户配置文件 每个用户都可以创建评论。我需要在每条评论旁边添加用户名 <%= @comment.user.name %>

在 user.rb 中

  has_many :comments, dependent: :destroy 

在comment.rb中

  belongs_to :users

在评论控制器中

before_action :find_comment ,only:[:show,:update,:edit,:destroy]

   def new
    @user =User.find(params[:id])
    @comment = @user.comments.build
  end

  def create
    @user =User.find(params[:id])
    @comment = @user.comments.build(comment_params)
    @comment.user = current_user
    if @comment.save
      redirect_to doctor_path(:id => @user.id)
    end
  end

private

  def find_comment
    @comment = Comment.find(params[:id])
  end

  def comment_params
    params.require(:comment).permit(:text)
  end

用户控制器

  def show
    @user = User.find(params[:id])
  end

用户 show.html.erb

<% for item in @user.comments %>
  <% if item.text.present? %>
    <%= item.text %><br>        
    <%= @comment.user.name %>
    <br><hr>
  <% end %>

我收到了这个错误

undefined method `user' for nil:NilClass

【问题讨论】:

  • 我不清楚你的问题是什么。你想用电子邮件做什么?
  • @jamesc 我想为评论添加用户名。例如,我正在写评论,我的名字是 nourza。我的名字会显示在评论后面
  • 您需要在某处使用comment.user.name,但您没有提供足够的信息来给出明确的答案
  • @jamesc 你需要什么信息?
  • 我得到了这个错误 undefined method `user' for nil:NilClass

标签: ruby-on-rails database authentication devise comments


【解决方案1】:

你可以用你的 show 方法做相反的事情:

@comments = Comment.all

在您的show 视图中:

<% @comments.each do |comment| %>
  <%= comment.text %>
  <%= comment.user.name %>
<% end %>

由于您的问题不是很清楚,我会指定您是否只想显示用户发布的 cmets:

def show
  user_id = User.find(params[:id]).id
  @comments = Comment.where(user_id: user_id)
end

【讨论】:

  • 是的,我只想显示用户发布的 cmets。我应该如何在显示视图中添加?
  • 与我在第一个示例中@comments = Comment.all 时所做的相同,因为您只是在更改变量
  • 当我将此@cmets = Comment.all 添加到用户控制器显示时,我收到此错误 undefined method `user' for #<0x007fdee6f23ab0>
【解决方案2】:

只是一些快速开始的规则

  1. 一个用户有很多个cmet,这将是用户和用户发表的评论之间的关系。 你已经有了这个

  2. 一个用户有许多个人资料 cmets,这是用户与其个人资料中留给该用户的 cmets 之间的关系

现在您有了区别,事情开始变得更加清晰。

首先创建一个外部参照表作为用户和已为配置文件留下的 cmets 之间的通道,并将其命名为 profile_comments 这个profile_comments 表需要一个user_id 和一个整数类型的comment_id 来存储来自用户和cmets 表的主键,其中user_id 是用户的ID,该用户在他们的简介

您现在可以设置具有以下关系的profile_comment 模型

belongs_to comment
belongs_to user

所以现在您需要将您的用户模型关系更改为以下

用户.rb

  has_many :comments
  has_many :profile_comments, dependent: :destroy

comment.rb

  belongs_to :user #not users as you have defined in your question
  has_many :profile_comments, dependent: :destroy

而新的profile_comment.rb 模型需要两个belongs_to 子句用于评论和用户

profile_comment.rb

belongs_to :user
belongs_to :comment

现在,当您创建评论时,您需要分配给用户以及 profile_comment

所以现在你的 cmets 控制器需要设置这些关系而不是

before_action :find_comment ,only:[:show,:update,:edit,:destroy]

   def new
    @user =User.find(params[:id])
    @comment = @user.comments.build
  end

  def create
    @user =User.find(params[:id])
    @comment = @user.comments.build(comment_params)
    @comment.user = current_user
    if @comment.save
      redirect_to doctor_path(:id => @user.id)
    end
  end 

你需要这样的东西

  def create
    @user =User.find(params[:id])
    @comment = current_user.comments.build(comment_params)
    @profile_comment = ProfileComment.new
    @user.profile_comment < @profile_comment
    @comment.profile_comment < @profile_comment
    if @comment.save
      redirect_to doctor_path(:id => @user.id)
    end
  end 

您的更新操作也需要相应更改

现在在你的视野中,而不是

<% for item in @user.comments %>
  <% if item.text.present? %>
    <%= item.text %><br>        
    <%= @comment.user.name %>
    <br><hr>
  <% end %>
<% end %>

你想要这个,它有点复杂,因为你需要从个人资料评论到评论,然后到创建评论的用户

<% @user.profile_comments.each do | profile_comment |%>
  <%comment = profile_comment.comment%>
  <% if comment.text.present? %>
    <%= comment.text %><br>
    <%if comment.user.blank?%>
      No user assigned to this comment
    <%else%>
      <%= comment.user.name #or email or whatever%>
    <%end%>
    <br><hr>
  <% end %>
<% end %>

虽然 text 是保留字并且是实际的列类型,所以您可能希望将列名 text 更改为其他内容

如有任何关于此的问题,请随时与我联系,但我不会在接下来的 24 小时内出现。希望它清楚并帮助您了解初始设置出了什么问题

在控制器和视图中,我假设current_user 是发表评论的人,@user 是被评论的人。如果我错了就换那一轮

【讨论】:

  • 不,我希望每条评论都只显示用户发布的 cmets。例如 x 用户发表评论以显示 x 用户名。您的用户发表评论以显示您的姓名。
  • 任何用户都可以在任何个人资料中发表评论。我希望每个用户名都显示在评论后面。喜欢这些 cmets。
  • @nourza 仍然感到困惑,所以在查看个人资料时,您想查看该个人资料上的 cmets 以及发表评论的用户的姓名?这就是我给你的
  • 是的,这就是我想要的,但结果却是另外一回事。结果为所有 cmets 提供了配置文件的相同用户名。例如,配置文件 x 在 x 名称下有 cmets,而不是在发布名称下有 cmets
  • @nourza,现在它变得更加清晰了。你能发布你的模型吗,特别是关系,我可能需要更多,但那应该做一分钟
猜你喜欢
  • 1970-01-01
  • 2018-03-24
  • 1970-01-01
  • 2013-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多