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?
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 %>