更新:带有nested_fields和多个has_one的Rails表单

时间:2018-08-10 00:32:35

标签: ruby-on-rails nested-attributes accepts-nested-attributes

我有以下型号:

class Property < ApplicationRecord

  # Other validations 

  has_one :address
  accepts_nested_attributes_for :address, update_only: true

end 

class Address < ApplicationRecord
  has_one :country
  has_one :state
  has_one :city
  has_one :suburb

  belongs_to :property
end

countrystatecitysuburb的关系都彼此之间用has_manybelongs_to链接。

问题是:

在我的properties/_form.html.erb文件上,我试图使用嵌套fields_for:address以及在嵌套字段中使用options_from_collection_for_select创建地址。下面的代码:

  <fieldset>
    <div class="form-group">
      <%= form.label :address, "Dirección", class: "col-sm-2 control-label"%>
      <div class="col-sm-9">
          <%= form.fields_for :address do |ff| %>
            <div class="row">
              <div class="col-sm-4">
                <select class="form-control" name="property[address_attributes][country_id]" >
                  <%= options_from_collection_for_select(Country.all, :id, :name) %>
                </select>
              </div>
              <div class="col-sm-4">
                <select class="form-control"  name="property[address_attributes][state_id]" >
                  <%= options_from_collection_for_select(State.all, :id, :name) %>
                </select>
              </div>
              <div class="col-sm-4">
                <select class="form-control"  name="property[address_attributes][city_id]" >
                  <%= options_from_collection_for_select(City.all, :id, :name) %>
                </select>

在提交时出现此错误:

submit error

更新1

因此,我对此做了一些更改:

<%= ff.select :country, options_from_collection_for_select(Country.all, :id, :name) %>

关于我所有的关系,现在我得到的错误是:

Country(#70194352893700) expected, got "1" which is an instance of String(#70194311847460)

Country expected got whis is an instance of String

更新2:

这是我的schema.rb上描述我的addresses表的代码:

  create_table "addresses", force: :cascade do |t|
    t.string "street"
    t.integer "number"
    t.integer "zip_code"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "property_id"
    t.integer "countries_id"
    t.integer "states_id"
    t.integer "cities_id"
    t.integer "suburbs_id"
    t.index ["cities_id"], name: "index_addresses_on_cities_id"
    t.index ["countries_id"], name: "index_addresses_on_countries_id"
    t.index ["property_id"], name: "index_addresses_on_property_id"
    t.index ["states_id"], name: "index_addresses_on_states_id"
    t.index ["suburbs_id"], name: "index_addresses_on_suburbs_id"
  end

1 个答案:

答案 0 :(得分:1)

我猜想,即使您的address 模型country_id 上也没有Address字段>说应该有关系。

您可以通过查看db/schema.rb进行验证。

如果address表缺少该列,则可以在命令行上执行以下操作以添加该列:

rails g migration add_country_id_to_addresses country:belongs_to

然后运行rake db:migrate

上面的说明中包含了一些“魔术”,所以这里有一个解释。

rails g migration告诉Rails生成数据库迁移。

add_country_to_addresses是迁移的名称。如果您通过snake_case进入CamelCase,也可以使用AddCountryToAddresses。如果rails在迁移名称的末尾找到_to_*(或To*),它可以推断要为其生成迁移的表的名称。在这种情况下,addresses表。

country:belongs_to告诉Rails迁移应将addresses表链接到countries表。它将添加一个country_id列,并且(取决于您的数据库设置)将为新列生成一个索引和/或外键。

更新

您的db/schema.rb表明您实际上在该表上没有country_id字段。您有一个countries_id字段。

要解决此问题,您可以生成空白迁移:

rails g migration rename_countries_id_on_addresses

然后将迁移内容编辑为包含

change_table :addresses do |t|
  t.rename :countries_id, :country_id
end

然后运行迁移:

rake db:migrate

您可以在此处找到有关通过迁移来更改表的更多信息:https://guides.rubyonrails.org/active_record_migrations.html#changing-tables