Ruby on rails 3 的嵌套表单属性有一个关系
我已经搜索了SO和Internet有一段时间了,但还没有弄明白这一点。我有一个模型项目,总是有一次设置。没有涉及联接表 在新的项目表单中,我尝试使用嵌套属性创建TimerSetting记录。我搞不懂这部分 模型中的相关代码: 项目模型:Ruby on rails 3 的嵌套表单属性有一个关系,ruby-on-rails-3,ruby-on-rails-3.2,Ruby On Rails 3,Ruby On Rails 3.2,我已经搜索了SO和Internet有一段时间了,但还没有弄明白这一点。我有一个模型项目,总是有一次设置。没有涉及联接表 在新的项目表单中,我尝试使用嵌套属性创建TimerSetting记录。我搞不懂这部分 模型中的相关代码: 项目模型: class Project < ActiveRecord::Base attr_accessible :timer_setting_id has_one :timer_setting accepts_nested_attribut
class Project < ActiveRecord::Base
attr_accessible :timer_setting_id
has_one :timer_setting
accepts_nested_attributes_for :timer_setting
end
他认为:
<%= form_for @project, {remote: true, format: 'json'} do |f| %>
... other stuff ...
<%= f.fields_for :timer_setting do |ts| %>
Rounding Method <%= ts.check_box :rounding_method %>
Round To <%= ts.text_field :round_to %>
<% end %>
<% end %>
它将呈现表单,但不会输出f.fields\u for:timer\u设置
块中的字段
任何建议/帮助都将不胜感激 您的计时器设置需要一个项目id。您的项目不需要计时器设置id,因为rails将通过计时器设置模型的项目id找到它
def new
@project.new
@project.build_timer_setting
end
<%= form_for @project, {remote: true, format: 'json'} do |f| %>
... other stuff ...
<%= f.fields_for :timer_setting do |ts| %>
Rounding Method <%= ts.check_box :rounding_method %>
Round To <%= ts.text_field :round_to %>
<% end %>
<% end %>
#@project.build_timer_setting