Ruby on Rails:如何使用 has_one 关系处理嵌套表单? [英] Ruby on Rails: How do I do nested forms with a has_one relationship?
本文介绍了Ruby on Rails:如何使用 has_one 关系处理嵌套表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
class PollOption < ActiveRecord::Base
belongs_to :poll
has_one :address
end
class Address < ActiveRecord::Base
belongs_to :user, :poll_options
apply_addresslogic :fields => [[:number, :street], :city, [:state, :zip_code]]
end
那些是我的相关模型.有任何想法吗?我只是需要一个很好的例子.
Those are my relevant models. Any ideas? I kinda just need a good example.
推荐答案
这应该回答:
http://archives.ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes
主要思想是在您的 PollOption 模型中声明 accepts_nested_attributes_for :address
并按照我提供的链接的第 2 步中的指示更改您的表单.
The main idea is to declare accepts_nested_attributes_for :address
in your PollOption model and to change your form as indicated in the step 2 of the link I provided.
另一个有用的链接:http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html
这篇关于Ruby on Rails:如何使用 has_one 关系处理嵌套表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文