关联USER_ID到注释模式? [英] Associate user_id to the comment model?
本文介绍了关联USER_ID到注释模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用设计进行验证,我想USER_ID关联到comment模型。它的工作原理,当我送的user_id作为参数,但我想自动设置当前logined用户的USER_ID到每个注释。我该怎么办呢?
#型号/ post.rb
类岗位<的ActiveRecord :: Base的
的has_many:评论
belongs_to的:类
结束
#型号/ comment.rb
一流的注释和LT;的ActiveRecord :: Base的
belongs_to的:交
结束
#型号/ category.rb
的has_many:帖子
#控制器/ comments_controller
类CommentsController<的ApplicationController
before_action:set_comment,只有:[:显示,编辑,:更新:灭]
before_action:的authenticate_user!
#GET /评论
#GET /comments.json
高清指数
@comments = Comment.all
结束
#GET /评论/ 1
#GET /comments/1.json
高清节目
结束
#GET /评论/新
高清新
@comment = Comment.new
结束
#GET /评论/ 1 /编辑
高清编辑
结束
#POST /评论
#POST /comments.json
DEF创建
@comment = Comment.new(comment_params)
respond_to代码做|格式|
如果@ comment.save
的format.html {redirect_to时@comment,通知:评论已成功创建 }
format.json {渲染:表演,状态:创建,地点:@comment}
其他
的format.html {渲染:新}
format.json {渲染JSON:@ comment.errors,状态:unprocessable_entity}
结束
结束
结束
#PATCH / PUT /评论/ 1
#PATCH / PUT /comments/1.json
DEF更新
respond_to代码做|格式|
如果@ comment.update(comment_params)
的format.html {redirect_to时@comment,通知:评论已成功更新 }
format.json {渲染:表演,状态:OK,地点:@comment}
其他
的format.html {渲染:编辑}
format.json {渲染JSON:@ comment.errors,状态:unprocessable_entity}
结束
结束
结束
#删除/评论/ 1
#删除/comments/1.json
DEF破坏
@ comment.destroy
respond_to代码做|格式|
的format.html {redirect_to时comments_url,通知:有人评论成功摧毁 }
format.json {头:} NO_CONTENT
结束
结束
私人
#使用回调分享共同的设置或动作之间的约束。
高清set_comment
@comment = Comment.find(PARAMS [:ID])
结束
#从不信任的参数从可怕的网络,只允许白名单通过。
高清comment_params
params.require(:评论).permit(:评级:评论,:的post_id,:USER_ID)
结束
结束
解决方案
使用关联用户的has_many的意见和
belongs_to的用户评论
高清新
@comment = current_user.comments.build
结束
高清创建
@comment = current_user.comments.build(comment_params)
结束
I use devise for authentication, I want to associate user_id to the comment model. It works when I send user_id as a parameter, But I want to set the current logined user's user_id automatically to the each comment. How can I do that?
#model/post.rb
class Post < ActiveRecord::Base
has_many :comments
belongs_to :category
end
#model/comment.rb
class Comment < ActiveRecord::Base
belongs_to :post
end
#model/category.rb
has_many :posts
#controllers/comments_controller
class CommentsController < ApplicationController
before_action :set_comment, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
# GET /comments
# GET /comments.json
def index
@comments = Comment.all
end
# GET /comments/1
# GET /comments/1.json
def show
end
# GET /comments/new
def new
@comment = Comment.new
end
# GET /comments/1/edit
def edit
end
# POST /comments
# POST /comments.json
def create
@comment = Comment.new(comment_params)
respond_to do |format|
if @comment.save
format.html { redirect_to @comment, notice: 'comment was successfully created.' }
format.json { render :show, status: :created, location: @comment }
else
format.html { render :new }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /comments/1
# PATCH/PUT /comments/1.json
def update
respond_to do |format|
if @comment.update(comment_params)
format.html { redirect_to @comment, notice: 'comment was successfully updated.' }
format.json { render :show, status: :ok, location: @comment }
else
format.html { render :edit }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
# DELETE /comments/1
# DELETE /comments/1.json
def destroy
@comment.destroy
respond_to do |format|
format.html { redirect_to comments_url, notice: 'comment was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_comment
@comment = Comment.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def comment_params
params.require(:comment).permit(:rating, :comment, :post_id, :user_id)
end
end
解决方案
use associations user has_many comments and
comments belongs_to user
def new
@comment = current_user.comments.build
end
def create
@comment = current_user.comments.build(comment_params)
end
这篇关于关联USER_ID到注释模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文