form_for 和 form_tag,什么时候用哪个? [英] form_for and form_tag, when to use which one?

查看:46
本文介绍了form_for 和 form_tag,什么时候用哪个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读一本 Rails 书籍,到目前为止它一直在使用 form_for 来创建表单.在为用户构建登录页面的新章节中,它使用 form_tag 代替.所以我想知道我们应该何时以及如何知道使用哪个?他也展示了这个 form_tag 是否只是一个教育性的事情?还是应该考虑更多的技术原因?

I am reading a Rails book and so far it has been using form_for to create the forms. In the new chapter that it is building a login page for users, it is using form_tag instead. So I wanted to know when and how we should know which one to use? Is it just a educational thing he has done to also show this form_tag too ? or there are more technical reasons that should be considered?

推荐答案

form_tag 只是创建一个表单.form_for 创建模型对象的表单.它们不可互换,您应该使用适合特定情况的那个.

form_tag simply creates a form. form_for creates a form for a model object. They are not interchangeable and you should use the one that is appropriate for a given case.

form_for 更容易用于为模型对象创建表单,因为它根据对象是新记录还是已保存的记录来确定要使用的 url 和使用的 http 方法记录.

form_for is a bit easier to use for creating forms for a model object because it figures out what url to use and what http method to use depending on whether the object is a new record or a saved record.

这篇关于form_for 和 form_tag,什么时候用哪个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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