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