发布会嵌入许多评论。
_id
是帖子的Integer
类型字段,但是String
一个 - 用于评论。
post.rb:
class Post
include Mongoid::Document
include Mongoid::Attributes::Dynamic
field :_id, type: Integer
field :title, type: String
field :content, type: String
embeds_many :comments
accepts_nested_attributes_for :comments
end
comment.rb:
class Comment
include Mongoid::Document
include Mongoid::Attributes::Dynamic
field :_id, type: String
field :content, type: String
embedded_in :post
end
posts_controller.rb:
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
# GET /posts
# GET /posts.json
def index
@posts = Post.all
end
# GET /posts/1
# GET /posts/1.json
def show
end
# GET /posts/new
def new
@post = Post.new
end
# GET /posts/1/edit
def edit
end
# POST /posts
# POST /posts.json
def create
@post = Post.new(post_params)
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: @post }
else
format.html { render :new }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: @post }
else
format.html { render :edit }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
@post.destroy
respond_to do |format|
format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:_id, :title, :content)
end
end
comments_controller.rb:
class CommentsController < ApplicationController
before_action :set_comment, only: [:show, :edit, :update, :destroy]
def index
@post = Post.find(params[:post_id])
@comments = @post.comments
end
def show
end
def new
@post = Post.find(params[:post_id])
@comment = @post.comments.new
end
def edit
@comment = @post.comments.find(params[:id])
end
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.new(comment_params)
respond_to do |format|
if @comment.save
format.html { redirect_to post_path(@post), notice: 'Comment was successfully created.' }
format.json { render :show, status: :created, post: @comment }
else
format.html { render :new }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
def update
end
def destroy
end
private
def set_comment
@post = Post.find(params[:post_id])
@comment = @post.comments.find(params[:id])
end
def comment_params
params.require(:comment).permit(:_id, :content)
end
end
当我添加评论时,它只保存评论的内容字段的值,而不保存我输入的_id。虽然这适用于帖子模型:
> db.posts.findOne()
{
"_id" : 23,
"title" : "First Post",
"content" : "Text of the 1st post",
"comments" : [
{
"content" : "Comment for the 1st post"
},
{
"content" : "2nd comment"
},
{
"content" : "asdfagzcbzcv"
}
]
}
我在github上有这个应用程序,可在https://github.com/tenzan/blog.git
公开答案 0 :(得分:0)
让_id字段清理:)
从post
Post.find( params[:post_id] )
获得的post.comments.find( params[:comment_id] )
对象,您可以post.comments[ params[:comment_id] ]
。
custom_id
没有优化(如果我正确理解你要做的事情)。
如果您真的希望向用户显示更清晰的评论ID,您可以自己实现acts_as_list
这样的字段或使用gem {{1}}。