0
我正在用sinatra構建博客帖子應用程序。每篇文章都會有很多評論。我可以創建Post
但comments
不想被創建,我不知道爲什麼。保存屬於sinatra的對象
main.rb的
get "/" do
@posts = Post.all
haml :index
end
post '/new/post' do
Post.create params['post']
redirect to('/')
end
post '/:id' do
Post.get(params[:id]).comments.create(params['comment'])
redirect to('/')
end
DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/development.db")
class Post
include DataMapper::Resource
property :id, Serial
property :title, String
property :content, String
property :photo, String
property :rating, Integer
has n, :comments, :constraint => :destroy
end
class Comment
include DataMapper::Resource
property :id, Serial
property :content, String
belongs_to :post
end
DataMapper.finalize
index.haml
%form.new{:action => "/new/post", :method => "POST"}
%input{:name => "post[title]", :type => "text"}/
%input{:name => "post[content]", :type => "text"}/
%input{:type => "submit", :value => "Publier"}/
%form.new{:action => "/#{post.id}", :method => "GET"}
%input{:name => "_method", :type => "hidden", :method => "POST"}
%input{:content => "comment[content]", :type => "text"}/
%input.button{:type => "submit", :value => "Commenter !"}
那麼我應該改變什麼? – Orsay
已更新的回答。 –