当必填字段不正确时,页面会重新加载,您会收到不正确的输入字段,并且表单 company_form 中的所有字段都重复。
用户模型(user.rb):
class User < ActiveRecord::Base
....
has_many :companies, :autosave => true
accepts_nested_attributes_for :companies
has_and_belongs_to_many :roles
def role?(role_name)
return !!self.roles.find_by_name(role_name)
end
def with_company
self.companies.build
self
end
end
公司型号:
class Company < ActiveRecord::Base
...
belongs_to :user
...
end
意见/设计/注册/new.html.haml:
= form_for(resource.with_company, :as => resource_name, :url => registration_path(resource_name), :html => { :class => 'form-horizontal'}) do |f|
...
= f.fields_for :companies do |company_form|
...
...