Ruby on Rails:符号作为 form_for 中的参数

发布于 2024-08-17 00:57:04 字数 343 浏览 3 评论 0原文

我了解在执行以下操作时传递给 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

去了角落 2024-08-24 00:57:04

使用符号将生成:

<form action="/users" method="post">

使用对象@user设置为新的你会得到:

<form action="/users" class="new_user" id="new_user" method="post">

如果你将@user设置为其他东西,它当然会改变生成的表单标签
...自己尝试一下。

最终,这一切都取决于您想要做什么。如果同一页面上有多个 :user 表单,您可能需要使用该符号。

Using the symbol will generate:

<form action="/users" method="post">

Using the object @user set to new you get:

<form action="/users" class="new_user" id="new_user" method="post">

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文