2

我正在尝试了解如何使用远程表单并使用 Post 模型 has_many Comments 的应用程序示例。

假设我已经使用通用 rails 脚手架来创建/设置 post/comment 模型、控制器、默认视图、路由等——我应该是什么app/views/posts/show.html样子?

具体我很困惑:

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

谢谢!

4

1 回答 1

5

假设你的帖子有_many评论......

routes.rb(嵌套资源)

resources :posts do
  resources :comments
end

在你的comments_controller

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

在表格中:

=form_for [@post, @comment], :remote => true do |f|
  =f.text_field :text
  =f.submit
于 2011-10-31T01:10:49.480 回答