2014-12-26 66 views
0

嵌入很多評論_id未保存爲嵌入文檔

_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

爲什麼你在模型中添加了_id字段,默認情況下會添加_id字段。 – pkrawat1

+0

我需要通過URL訪問嵌入式文檔:'/ posts /:post_id/comments /:id /',並且知道關於帖子ID和評論ID的信息會讓我知道我在訪問什麼嵌入式文檔。也許與帖子和評論的問題不是最好的... – Askar

回答

0

讓_id場清潔:)

從你通過了你的post對象Post.find(params[:post_id]),你可以做post.comments.find(params[:comment_id])

有沒有優化與post.comments[ params[:comment_id] ](如果我正確理解你在做什麼)。

如果你真的想給你的用戶顯示一個更清晰的評論ID,你可以自己實現一個字段,如custom_id或使用寶石acts_as_list