如何将嵌套属性与 form_for 的 select 方法一起使用?
我们有一个用户
模型,其中:has_one详细信息
。在用户的 form_for
中,我需要一个下拉菜单来选择用户详细信息的time_zone
。
我已经尝试过了
<% form_for @user do |f| %>
... user stuff ...
<%= f.select :"detail.time_zone", ...other args... %>
<% end %>
,但我收到了detail.time_zone 的 NoMethodError
错误。执行此操作的正确语法是什么 - 或者如果不可能以这种方式执行此操作,我应该如何执行?
We have a user
model which :has_one detail
. In a form_for
a user, I want a drop-down to select the user's details' time_zone
.
I've tried
<% form_for @user do |f| %>
... user stuff ...
<%= f.select :"detail.time_zone", ...other args... %>
<% end %>
but I get a NoMethodError
for detail.time_zone. What's the correct syntax for doing this - or if it's not possible to do it this way, how should I be doing it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要忘记使用
accepts_nested_attributes_for
在您的用户模型中:详细模型:
用户控制器:
用户新建/编辑视图:
Don't forget to use
accepts_nested_attributes_for
in your user model:Detail model:
Users controller:
User new/edit view:
为什么
f.select
后面有一个冒号?它还应该是<%=...
而不是<%..
假设您的表中有一个“time_zone”列,
其他资源:
Why is there a colon after
f.select
? Also it should be<%=...
and not<%..
Assuming you have a 'time_zone' column in your table,
Additional resource :