Rails 5:form_for 与 form_with [英] Rails 5: form_for vs form_with

查看:65
本文介绍了Rails 5:form_for 与 form_with的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Rails 5 引入了新的表单辅助方法 form_with.

Rails 5 has introduced new form helper method form_with.

它与 form_for 有何不同,什么时候使用更合适?

How does it differs with form_for and when is it more appropriate to use?

推荐答案

这实际上是为 Rails 5.1 做准备,其中应该只使用 form_with.它旨在替代 form_forform_tag 两种方法.

This is really in preparation for rails 5.1 where only form_with should be used. It's meant to serve as a replacement for the two methods form_for and form_tag.

form_forform_tag 非常相似,都允许您创建表单标签,但第一个使用模型的属性来构建创建或更新表单,而第二个简单地创建一个 HTML 表单标签,并将传递的 URL 作为操作.

form_for and form_tag in Rails were very similar, both allowed you to create a form tag but the first one uses model’s attributes to build create or update form, while the second one simply creates an HTML form tag with the passed URL as action.

这篇关于Rails 5:form_for 与 form_with的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆