Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails设置将用户名设计为注释的属性_Ruby On Rails_Postgresql_Activerecord_Devise - Fatal编程技术网

Ruby on rails Rails设置将用户名设计为注释的属性

Ruby on rails Rails设置将用户名设计为注释的属性,ruby-on-rails,postgresql,activerecord,devise,Ruby On Rails,Postgresql,Activerecord,Devise,我对rails还不熟悉,仍然在弄清楚哪些东西属于模型,哪些属于控制器。我正在创建一个属于文章的简单评论模型。我有一个属性:commenter,它是一个字符串。我想从当前用户(我正在使用Desive的登录功能)获取用户名,我会在控制器的创建方法中这样做吗 差不多 def create @post = Post.find(params[:post_id]) @comment.commenter = current_user.username @comment = @post.

我对rails还不熟悉,仍然在弄清楚哪些东西属于模型,哪些属于控制器。我正在创建一个属于文章的简单评论模型。我有一个属性:commenter,它是一个字符串。我想从当前用户(我正在使用Desive的登录功能)获取用户名,我会在控制器的创建方法中这样做吗

差不多

def create
    @post = Post.find(params[:post_id])
    @comment.commenter = current_user.username
    @comment = @post.comments.create(comment_params)
    redirect_to post_path(@post)
end
之后,您可以获取任何评论的所有用户详细信息:-

comment = Comment.find(#id_of_comment)
comment.username => #will return username because of delegation

如果您在用户和评论之间添加关联会更好,比如评论属于用户,用户有很多评论。您可以从comments中的user_id获取comment的用户table@ezweizig帖子和文章一样吗?
    def create
      @post = Post.find(params[:post_id])
      @comment = @post.comments.new(comment_params)
      @comment.user = current_user
      if @comment.save
        flash[:success] = "Comment saved successfully!"
        redirect_to post_path(@post)
      else
        flash[:error] = @comment.errors.full_messages.to_sentence
        redirect_to post_path(@post)
      end
    end
comment = Comment.find(#id_of_comment)
comment.username => #will return username because of delegation