Rails 3.1发布/评论博客模型 - 如何通过ajax提交评论?

Mar*_*tto 2 ruby forms ruby-on-rails ujs ruby-on-rails-3

我正在尝试学习使用远程表单并使用Post模型has_many评论的应用程序示例.

假设我使用通用rails脚手架来创建/设置帖子/评论模型,控制器,默认视图,路线等, - 我应该app/views/posts/show.html怎么样?

具体我很困惑:

  1. 评论表格应该在哪里发布?
  2. 应包括哪些参数?
  3. 我是否需要使用隐藏的属性,如 f.hidden_field :post_id, :value => @post.id

谢谢!

Jes*_*ott 5

假设你的帖子有很多评论......

routes.rb(嵌套资源)

resources :posts do
  resources :comments
end
Run Code Online (Sandbox Code Playgroud)

在你的comments_controller中

def create
  @post = Post.find(params[:post_id]
  @comment = @post.comment.build(params[:comment])
  if @comment.save
  ...
end
Run Code Online (Sandbox Code Playgroud)

形式如下:

=form_for [@post, @comment], :remote => true do |f|
  =f.text_field :text
  =f.submit
Run Code Online (Sandbox Code Playgroud)