Rails Model Association with form -
i trying achieve this:
- it has model called customer has details of customers name (first, middle , last) , telephone numbers (mobile , landline).
- it has model called address linked customer. model have details customers address (a number of address lines, country , post/zip code).
- a customers controller supports 2 pages. first list customers in database along associated address. second display form entry of new customer along address , allow creation of new customer , address record. should possible move either page other without having enter url in browser navigation bar. number of points of note follows...
this code:
migration:
class createaddresses < activerecord::migration def change create_table :addresses |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 |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
model:
class address < activerecord::base end class customer < activerecord::base has_one :address end
form:
<%= form_for([@address,@customer]) |f| %> <% if @customer.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@customer.errors.count, "error") %> prohibited customer being saved:</h2> <ul> <% @customer.errors.full_messages.each |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 %>
routes:
resources :addresses resources :customers end resources :customers resources :addresses end
i haven't changed in both controllers.
i getting error: undefined method `address_line1' #
i don't understand wrong. help? :( please
schema:
activerecord::schema.define(version: 20150722160725) create_table "addresses", force: :cascade |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 |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
Comments
Post a Comment