Mar*_*tto 2 ruby forms ruby-on-rails ujs ruby-on-rails-3
我正在尝试学习使用远程表单并使用Post模型has_many评论的应用程序示例.
假设我使用通用rails脚手架来创建/设置帖子/评论模型,控制器,默认视图,路线等, - 我应该app/views/posts/show.html怎么样?
具体我很困惑:
f.hidden_field :post_id, :value => @post.id 谢谢!
假设你的帖子有很多评论......
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)
| 归档时间: |
|
| 查看次数: |
2770 次 |
| 最近记录: |