双嵌套形式,设计和地理编码器未显示

double nested form with devise and geocoder not showing

我正在使用复杂的嵌套表单。 我有 3 个模型

地点

class Location < ActiveRecord::Base
  has_many :events
  has_many :profiles
  geocoded_by :address
end

简介

class Profile < ActiveRecord::Base
  belongs_to :user
  accepts_nested_attributes_for :user
  belongs_to :location
  accepts_nested_attributes_for :location
end

用户(设计)

class User < ActiveRecord::Base
  has_one :profile, dependent: :destroy
  accepts_nested_attributes_for :profile
end

在我的 User/new 视图中

= form_for @user, validate: true, url: user_registration_path, html: { class: 'new-user form-default' } do |f|
  = f.fields_for :profile do |p|
    = p.fields_for :location do |build|
      = build.text_field :address, :input_html =>{:id => 'geo-input-address', :value => current_user.city.name} 

我的问题是地址字段不可见,为什么不显示?

当您创建新用户时,该用户还没有个人资料或位置。 fields_for 迭代现有项目。

要么你 override the devise controllers,然后在 new 动作中你可以这样写:

def new
  @user = User.new
  @user.build_profile
  @user.profile.build_location
end

因此,您为 @user-对象提供了一个空配置文件和一个空位置,以便您的用户可以填写它。

简单一点,但也有点脏是直接在视图中做(然后你不需要覆盖设计控制器)。