我试图编辑一个字段,以便当用户创建帐户时,所有者布尔值设置为true,因此它进入数据库。我得到错误未定义方法`owner
= form_for @school do |f|
= f.label :school_name, "School Name"
= f.text_field :school_name, :class => 'form-control'
= f.label :latitude, "Latitude"
= f.text_field :latitude, :class => 'form-control'
= f.label :longitude, "Longitude"
= f.text_field :longitude, :class => 'form-control'
= f.label :radius, "Radius"
= f.text_field :radius, :class => 'form-control'
= f.fields_for :admins do |ff|
= ff.label :email
= ff.text_field :email, :class => 'form-control'
= ff.label :password
= ff.password_field :password, :class => 'form-control'
= ff.label :password_confirmation
= ff.password_field :password_confirmation, :class => 'form-control'
= f.submit :class => 'submit-button'
def new
@school = School.new
@school.admins.build
@school.admins.owner = true
end
def create
@school = School.new(school_params)
if @school.save
redirect_to :action => 'show', :id => @school
else
render 'new'
end
答案 0 :(得分:0)
由于ArrayAdapter
是多个管理员的集合,因此它没有@school.admins
方法,该方法存在于该集合中的各个管理对象上。
答案 1 :(得分:0)
当你致电@school.admins.build
时,它会返回一个新的Admin实例,但你没有对该实例做任何事情。您需要将其放在变量中,然后为其owner
属性赋值。这将是这样的:
@school = School.new
@admin = @school.admins.build
@admin.owner = true
但build
方法采用属性参数,因此您可以将其缩短为:
@school = School.new
@admin = @school.admins.build(owner: true)
不要忘记你还需要在某个时候保存这个对象。