0

Railsとmongoは初めてです。投稿に多くのコメントを含めることができる単純なブログ ページを作成しようとしています。Post Controller は、私が問題を抱えている場所です。既に公開された投稿のコメントを保存および取得する方法がわかりません。

投稿モデル

class Post
 include Mongoid::Document
 field :title, :type => String
 field :body, :type => String
 has_many :comments
end

コメントモデル

class Comment
 include Mongoid::Document
 field :content, :type => String
 belongs_to :post
end

現在の Post コントローラーは投稿を保存できますが、コメントは保存できません。

class PostsController < ApplicationController

  def show

@post = Post.find(params[:id])
@comment=Comment.new
respond_to do |format|
  format.html # show.html.erb
  format.xml  { render :xml => @post }
end

end

def new
  @post = Post.new

  respond_to do |format|
  format.html # new.html.erb
  format.xml  { render :xml => @post }
 end
end

def create
@post = Post.new(params[:post])

respond_to do |format|
  if @post.save
    format.html { redirect_to(@post, :notice => 'Post was successfully created.') }
    format.xml  { render :xml => @post, :status => :created, :location => @post }
  else
    format.html { render :action => "new" }
    format.xml  { render :xml => @post.errors, :status => :unprocessable_entity }
  end
end
end
4

1 に答える 1

1

Mongoidは、この場合、関係のアクセサーを提供します

posts.comments
posts.comments = [ Comment.new ]
comment.post
comment.post = Post.new
post.comments << Comment.new
post.comments.push(Comment.new)
...

http://mongoid.org/docs/relations/embedded/1-n.htmlを参照してください

変更された投稿/表示アクションは次のとおりです。

  def show

    @post = Post.find(params[:id])
    @comment = @post.comments.all.to_a
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @post }
    end

  end

これが新しい投稿/コメントアクションです。

  def comment
    @post = Post.find(params[:id])
    comment = Comment.create(params[:comment])
    @post.comments << comment
    @comment = @post.comments.all.to_a

    respond_to do |format|
      format.html { render :action => "show" }
      format.xml  { render :xml => @post }
    end
  end

app / views / posts / show.html.erb

<pre>
Post
  <%= @post.inspect %>
Comments
  <%= @comment.inspect %>
</pre>

tests /functional / posts_controller_test.rb

require 'test_helper'

class PostsControllerTest < ActionController::TestCase

  def setup
    Post.delete_all
    Comment.delete_all
    @param = {
        post: { title: 'Mongoid relations', body: 'The relation has auto-generated methods' },
        comment: { content: 'a piece of cake' }
    }
  end

  test "show" do
    post = Post.create(@param[:post])
    get :show, id: post.id
    assert_response(:success)
  end

  test "new" do
    get :new
    assert_response(:success)
  end

  test "create" do
    get :create, @param
    assert_response(:success)
  end

  test "comment" do
    post = Post.create(@param[:post])
    get :comment, { id: post.id }.merge(@param)
    assert_response(:success)
    assert_equal(1, Post.count)
    assert_equal(1, Comment.count)
    assert_equal(@param[:comment][:content], Post.first.comments.first.content)
  end

end
于 2012-05-17T16:43:41.573 に答える