如何将嵌套属性与 form_for 的 select 方法一起使用?

发布于 2024-09-07 00:42:36 字数 415 浏览 0 评论 0原文

我们有一个用户模型,其中: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 技术交流群。

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

发布评论

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

评论(2

Bonjour°[大白 2024-09-14 00:42:36

不要忘记使用 accepts_nested_attributes_for在您的用户模型中:

class User < ActiveRecord::Base
  has_one :detail
  accepts_nested_attributes_for :detail
end

详细模型:

class Detail < ActiveRecord::Base
  belongs_to :user
end

用户控制器:

class UsersController < ActionController::Base
  def new
    @user = User.new
    @user.build_detail
  end
end

用户新建/编辑视图:

<% form_for @user do |u| %>
  <% u.fields_for :detail do |d| %>
    <%= d.select :country, Country.all.map { |c| [c.name, c.id] }
    <%= d.time_zone_select :time_zone %>
  <% end %>
<% end %>

Don't forget to use accepts_nested_attributes_for in your user model:

class User < ActiveRecord::Base
  has_one :detail
  accepts_nested_attributes_for :detail
end

Detail model:

class Detail < ActiveRecord::Base
  belongs_to :user
end

Users controller:

class UsersController < ActionController::Base
  def new
    @user = User.new
    @user.build_detail
  end
end

User new/edit view:

<% form_for @user do |u| %>
  <% u.fields_for :detail do |d| %>
    <%= d.select :country, Country.all.map { |c| [c.name, c.id] }
    <%= d.time_zone_select :time_zone %>
  <% end %>
<% end %>
伴梦长久 2024-09-14 00:42:36

为什么f.select后面有一个冒号?它还应该是 <%=... 而不是 <%..

假设您的表中有一个“time_zone”列,

<% form_for @user do |f| %>
    ... user stuff ...
    
    # args: user object, time_zone method, prioritizing zones(separating list), default
    <%= f.time_zone_select :time_zone, nil, :default => "Pacific Time (US & Canada)", :include_blank => false %>
<% end %>

其他资源:

http://railscasts.com/episodes/106 -time-zones-in-rails-2-1

http://railsontherun.com/2007/12/21/timezone-选择/

Why is there a colon after f.select? Also it should be <%=... and not <%..

Assuming you have a 'time_zone' column in your table,

<% form_for @user do |f| %>
    ... user stuff ...
    
    # args: user object, time_zone method, prioritizing zones(separating list), default
    <%= f.time_zone_select :time_zone, nil, :default => "Pacific Time (US & Canada)", :include_blank => false %>
<% end %>

Additional resource :

http://railscasts.com/episodes/106-time-zones-in-rails-2-1

http://railsontherun.com/2007/12/21/timezone-selection/

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