Ruby on Rails:符号作为 form_for 中的参数
我了解在执行以下操作时传递给 form_for
方法的内容:
<% form_for(@user) do |f| %> ... <% end %>
如果在控制器中设置了 @user
。这是很明显的。
但是,当我们传递 :user
时会发生什么,正如我在许多示例中看到的那样?
<% form_for(:user) do |f| %> ... <% end %>
我什么时候应该使用符号版本?
I understand what is passed to the form_for
method when doing something like :
<% form_for(@user) do |f| %> ... <% end %>
if @user
is set in the controller. This is pretty obvious.
But what happens when we pass :user
, as I have seen in many examples ?
<% form_for(:user) do |f| %> ... <% end %>
When should I use the symbol version ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用符号将生成:
使用对象@user设置为新的你会得到:
如果你将@user设置为其他东西,它当然会改变生成的表单标签
...自己尝试一下。
最终,这一切都取决于您想要做什么。如果同一页面上有多个 :user 表单,您可能需要使用该符号。
Using the symbol will generate:
Using the object @user set to new you get:
If you set @user to something else it will change the form tag generated, of course
... try it out for yourself.
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.