Ruby on Rails:将符号作为form_for中的参数 [英] Ruby on Rails : symbol as argument in form_for
问题描述
我了解执行类似操作时传递给form_for
方法的内容:
I understand what is passed to the form_for
method when doing something like :
<% form_for(@user) do |f| %> ... <% end %>
如果在控制器中设置了@user
.这很明显.
if @user
is set in the controller. This is pretty obvious.
但是,正如我在许多示例中看到的那样,当我们通过:user
时会发生什么?
But what happens when we pass :user
, as I have seen in many examples ?
<% form_for(:user) do |f| %> ... <% end %>
何时应使用符号版本?
推荐答案
使用该符号将生成:
<form action="/users" method="post">
使用将@user设置为new的对象,您将得到:
Using the object @user set to new you get:
<form action="/users/create" class="new_user" id="new_user" method="post">
如果将@user设置为其他名称,则它将更改生成的form标记 ...亲自尝试一下.
If you set @user to something else it will change the form tag generated, of course ... try it out for yourself.
最后,这完全取决于您要执行的操作.如果在同一页面上有多个:user表单,则可能要使用该符号.
In the end it all depends what you're trying to do. You might want to use the symbol if you have multiple :user forms on the same page.
这篇关于Ruby on Rails:将符号作为form_for中的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!