日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Polymorphic form--多态表单

發布時間:2025/4/14 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Polymorphic form--多态表单 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一個ruby on rails項目,用戶和公司的模型都有地址。

我要創建一個地址表,包含用戶和公司表的引用,比直接做下去要好一點,這回讓我的數據庫設計保持干凈。

我的第一印象是,這似乎很難實現,外面所有的討論及教程都只說明了在model如何設置,但是并沒有說明在controller和view如何使用它。我好一頓放狗,也沒有得到太多的幫助。

令我感到驚喜是其實在rails設置并使用多態表單是很簡單的。

首先依然是先設置model結構:

01class Company< ActiveRecord::Base
02??has_one :address, :as =>; :addressable, :dependent => :destroy
03end
04?
05class User < ActiveRecord::Base
06??has_one :address, :as => :addressable, :dependent => :destroy
07end
08?
09class Address < ActiveRecord::Base
10??belongs_to :addressable, :polymorphic => true
11end

?

接下來是創建一個Address表來保存地址:

01class CreateAddresses < ActiveRecord::Migration
02??def self.up
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
11?
12??????t.timestamps
13????end
14??end
15?
16??def self.down
17????drop_table :addresses
18??end
19end

?

接下來是controller,你只需要修改controller中的"new","create","edit","update"四個action,好讓需要的時候可以訪問和修改address。

01class CompaniesController < ApplicationController
02?
03??def new
04????@company = Company.new
05????@company.address = Address.new
06??end
07?
08??def edit
09????@company = Company.find(params[:id])
10????@company.address = Address.new unless @company.address != nil
11??end
12?
13??def create
14????@company = Company.new(params[:company])
15????@company.address = Address.new(params[:address])
16?
17????if @company.save
18??????@company.address.save
19??????flash[:notice] = 'Company was successfully created.'
20??????redirect_to(@company)
21????else
22??????render :action => 'new'
23????end
24??end
25?
26??def update
27????@company = Company.find(params[:id])
28?
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)
33????else
34??????render :action => 'edit'
35????end
36??end
37end

?

最后一件事是讓address在表單中可以正常工作,我們這里使用field_for方法:

01<% form_for(@company) do |f| %>
02????<%= f.error_messages %>
03<dl>
04????????<%= f.text_field :name %>
05????????<%= f.text_field :telephone %>
06????????<%= f.text_field :fax %>
07????????<%= f.text_field :website_url %>
08????</dl>
09?
10????<% fields_for(@company.address) do |address_fields| %>
11????????<%= address_fields.hidden_field :addressable_id %>
12????????<%= address_fields.hidden_field :addressable_type %>
13<dl>
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 %>
19????????</dl>
20?
21????<% end %>
22<% end %>

?

到這就應該可以正常工作了。

有人要問了,如果我去的了address對象,能否反向取得Company或者User對象呢?答案當然是肯定的。

?

1@address = Address.find(params[:id])
2@address.addressable
3這樣就可以訪問了。

轉載于:https://www.cnblogs.com/wangyuyu/p/3312332.html

總結

以上是生活随笔為你收集整理的Polymorphic form--多态表单的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。