形式为

时间:2015-07-22 15:23:31

标签: ruby-on-rails forms ruby-on-rails-4 associations

我正在努力实现这一目标:

  1. 它有一个名为Customer的模型,其中包含客户名称的详细信息 (第一,中间和最后)和电话号码(移动电话和固定电话)。
  2. 它有一个名为Address的模型,该模型链接到Customer。这个模型 将提供客户地址的详细信息(许多地址行, 国家/地区和邮政编码)。
  3. 支持两页的客户控制器。第一个将列出 数据库中的所有客户及其相关地址。 第二个将显示一个新客户进入的表格 以他们的地址,并允许创建一个新的客户和 地址记录。应该可以从任一页面移动到 其他而无需在浏览器导航栏中输入URL。 关于这一点的一些注意事项如下......
  4. 这是我的代码:

    迁移:

    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
    

0 个答案:

没有答案