form_for和form_tag之间的区别? [英] Difference between form_for and form_tag?
问题描述
我在我的应用程序中使用了这个宝石,但我不确定不同的实现选项gem:
$ b
- form_for
- form_tag with block
- form_tag without block
任何人都可以澄清吗?我知道 form_for
用于你想与一个模型交互的情况,但是另外两个呢?
差异很微妙,但很重要。 form_for
是一个更高级的工具,可以生成一个用于生成表单元素的对象:
<%form_for(@foo)do | form | %GT;
<%= form.text_field(:bar)%>
<%end%>
< form>
标签中,你可以在块中放入东西:
<%form_tag do%>
<%= text_field_tag(:bar,'bar_value')%>
<%end%>
请注意, form_for
方法处理抓取值从你的模型,至少会尝试将表单路由到适当的操作。使用 form_tag
,您对所有事情负责,因为它不会假设您正在做什么。
I used this gem in my application, but I'm not sure the difference between the different implementation options for the gem:
- form_for
- form_tag with block
- form_tag without block
Can anyone clarify? I understand that form_for
is used when you wish to interact with a model, but what about the other two?
The differences are subtle, but important. form_for
is a more advanced tool that yields an object you use to generate your form elements:
<% form_for(@foo) do |form| %>
<%= form.text_field(:bar) %>
<% end %>
The form_tag
method is much more primitive and just emits a tag. If you want to put things inside of the <form>
tag that's emitted, you put things inside the block:
<% form_tag do %>
<%= text_field_tag(:bar, 'bar_value') %>
<% end %>
Note that the form_for
method handles grabbing values from your model, and will at least try to route the form to the appropriate action. With form_tag
you are responsible for everything as it makes no assumptions about what you're doing.
这篇关于form_for和form_tag之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!