生活随笔
收集整理的這篇文章主要介紹了
Polymorphic form--多态表单
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一個(gè)ruby on rails項(xiàng)目,用戶和公司的模型都有地址。
我要?jiǎng)?chuàng)建一個(gè)地址表,包含用戶和公司表的引用,比直接做下去要好一點(diǎn),這回讓我的數(shù)據(jù)庫設(shè)計(jì)保持干凈。
我的第一印象是,這似乎很難實(shí)現(xiàn),外面所有的討論及教程都只說明了在model如何設(shè)置,但是并沒有說明在controller和view如何使用它。我好一頓放狗,也沒有得到太多的幫助。
令我感到驚喜是其實(shí)在rails設(shè)置并使用多態(tài)表單是很簡單的。
首先依然是先設(shè)置model結(jié)構(gòu):
| 01 | class Company< ActiveRecord::Base |
| 02 | ??has_one :address, :as =>; :addressable, :dependent => :destroy |
| 05 | class User < ActiveRecord::Base |
| 06 | ??has_one :address, :as => :addressable, :dependent => :destroy |
| 09 | class Address < ActiveRecord::Base |
| 10 | ??belongs_to :addressable, :polymorphic => true |
?
接下來是創(chuàng)建一個(gè)Address表來保存地址:
| 01 | class CreateAddresses < ActiveRecord::Migration |
| 03 | ????create_table :addresses do |t| |
| 04 | ??????t.string :street_address1, :null => false |
| 05 | ??????t.string :street_address2 |
| 06 | ??????t.string :city, :null => false |
| 07 | ??????t.string :region, :null => false |
| 08 | ??????t.string :postcode, :null => false, :limit => 55 |
| 09 | ??????t.integer :addressable_id, :null => false |
| 10 | ??????t.string :addressable_type, :null => false |
| 17 | ????drop_table :addresses |
?
接下來是controller,你只需要修改controller中的"new","create","edit","update"四個(gè)action,好讓需要的時(shí)候可以訪問和修改address。
| 01 | class CompaniesController < ApplicationController |
| 04 | ????@company = Company.new |
| 05 | ????@company.address = Address.new |
| 09 | ????@company = Company.find(params[:id]) |
| 10 | ????@company.address = Address.new unless @company.address != nil |
| 14 | ????@company = Company.new(params[:company]) |
| 15 | ????@company.address = Address.new(params[:address]) |
| 18 | ??????@company.address.save |
| 19 | ??????flash[:notice] = 'Company was successfully created.' |
| 20 | ??????redirect_to(@company) |
| 22 | ??????render :action => 'new' |
| 27 | ????@company = Company.find(params[:id]) |
| 29 | ????if @company.update_attributes(params[:company]) |
| 30 | ??????@company.address.update_attributes(params[:address]) |
| 31 | ??????flash[:notice] = 'Company was successfully updated.' |
| 32 | ??????redirect_to(@company) |
| 34 | ??????render :action => 'edit' |
?
最后一件事是讓address在表單中可以正常工作,我們這里使用field_for方法:
| 01 | <% form_for(@company) do |f| %> |
| 02 | ????<%= f.error_messages %> |
| 04 | ????????<%= f.text_field :name %> |
| 05 | ????????<%= f.text_field :telephone %> |
| 06 | ????????<%= f.text_field :fax %> |
| 07 | ????????<%= f.text_field :website_url %> |
| 10 | ????<% fields_for(@company.address) do |address_fields| %> |
| 11 | ????????<%= address_fields.hidden_field :addressable_id %> |
| 12 | ????????<%= address_fields.hidden_field :addressable_type %> |
| 14 | ????????????<%= address_fields.text_field :street_address1 %> |
| 15 | ????????????<%= address_fields.text_field :street_address2 %> |
| 16 | ????????????<%= address_fields.text_field :city %> |
| 17 | ????????????<%= address_fields.text_field :region %> |
| 18 | ????????????<%= address_fields.text_field :postcode %> |
?
到這就應(yīng)該可以正常工作了。
有人要問了,如果我去的了address對象,能否反向取得Company或者User對象呢?答案當(dāng)然是肯定的。
?
| 1 | @address = Address.find(params[:id]) |
轉(zhuǎn)載于:https://www.cnblogs.com/wangyuyu/p/3312332.html
總結(jié)
以上是生活随笔為你收集整理的Polymorphic form--多态表单的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。