我正在努力实现这一目标:
这是我的代码:
迁移:
class CreateAddresses < ActiveRecord::Migration
def change
create_table :addresses do |t|
t.string :address_line1
t.string :address_line2
t.string :address_line3
t.string :city
t.string :county_province
t.string :zip_or_postcode
t.string :country
t.string :customer_id
t.timestamps null: false
end
create_table :customers do |t|
t.string :first_name
t.string :middle_name
t.string :last_name
t.integer :mobile_number
t.integer :landline_number
t.timestamps null: false
end
end
end
型号:
class Address < ActiveRecord::Base
end
class Customer < ActiveRecord::Base
has_one :address
end
形式:
<%= form_for([@address,@customer]) do |f| %>
<% if @customer.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@customer.errors.count, "error") %> prohibited this customer from being saved:</h2>
<ul>
<% @customer.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :first_name %><br>
<%= f.text_field :first_name %>
</div>
<div class="field">
<%= f.label :middle_name %><br>
<%= f.text_field :middle_name %>
</div>
<div class="field">
<%= f.label :last_name %><br>
<%= f.text_field :last_name %>
</div>
<div class="field">
<%= f.label :mobile_number %><br>
<%= f.number_field :mobile_number %>
</div>
<div class="field">
<%= f.label :landline_number %><br>
<%= f.number_field :landline_number %>
</div>
<div class="field">
<%= f.label :address_line1 %><br>
<%= f.text_field :address_line1 %>
</div>
<div class="field">
<%= f.label :address_line2 %><br>
<%= f.text_field :address_line2 %>
</div>
<div class="field">
<%= f.label :address_line3 %><br>
<%= f.text_field :address_line3 %>
</div>
<div class="field">
<%= f.label :city %><br>
<%= f.text_field :city %>
</div>
<div class="field">
<%= f.label :county_province %><br>
<%= f.text_field :county_province %>
</div>
<div class="field">
<%= f.label :zip_or_postcode %><br>
<%= f.text_field :zip_or_postcode %>
</div>
<div class="field">
<%= f.label :country %><br>
<%= f.text_field :country %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
路线:
resources :addresses do
resources :customers
end
resources :customers do
resources :addresses
end
我的两个控制器里都没有改变任何东西。
我收到此错误:未定义的方法`address_line1&#39;对于#
我不明白我错在哪里。有帮助吗? :(请
架构:
ActiveRecord::Schema.define(version: 20150722160725) do
create_table "addresses", force: :cascade do |t|
t.string "customer_id"
t.string "address_line1"
t.string "address_line2"
t.string "address_line3"
t.string "city"
t.string "county_province"
t.string "zip_or_postcode"
t.string "country"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "addresses", ["customer_id"], name: "index_addresses_on_customer_id"
create_table "customers", force: :cascade do |t|
t.string "first_name"
t.string "middle_name"
t.string "last_name"
t.integer "mobile_number"
t.integer "landline_number"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end