我正在尝试了解如何使用远程表单并使用 Post 模型 has_many Comments 的应用程序示例。
假设我已经使用通用 rails 脚手架来创建/设置 post/comment 模型、控制器、默认视图、路由等——我应该是什么app/views/posts/show.html
样子?
具体我很困惑:
- 评论表应该发到哪里?
- 应该包括哪些参数?
- 我是否需要使用隐藏属性,例如
f.hidden_field :post_id, :value => @post.id
谢谢!
我正在尝试了解如何使用远程表单并使用 Post 模型 has_many Comments 的应用程序示例。
假设我已经使用通用 rails 脚手架来创建/设置 post/comment 模型、控制器、默认视图、路由等——我应该是什么app/views/posts/show.html
样子?
具体我很困惑:
f.hidden_field :post_id, :value => @post.id
谢谢!
假设你的帖子有_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