試試這個。
我們假設有orders
和customers
表,並且您希望從訂單表單中對客戶執行CRUD操作。
客戶模式很簡單
class Customer < ActiveRecord::Base
attr_accessible :name
end
Order
模型必須對所有客戶的屬性(attr_accessor
構建)提供虛擬屬性。通過回調來提供customers
的CRUD。驗證也可以使用。
class Order < ActiveRecord::Base
attr_accessor :customer_name
attr_accessible :description, :number, :customer_name
belongs_to :customer
validates_presence_of :number
validates_presence_of :description
validates_presence_of :customer_name
before_save :save_customer
after_find :find_customer
after_destroy :destroy_customer
protected
def save_customer
if self.customer
self.customer.name = self.customer_name
else
self.customer = Customer.create(name: self.customer_name)
end
self.customer.save
end
def find_customer
self.customer_name = self.customer.name
end
def destroy_customer
self.customer.destroy
end
end
Order model的示例網格。
class Orders < Netzke::Basepack::Grid
def configure(c)
super
c.model = 'Order'
c.items = [
:description,
:number,
:customer_name
]
c.enable_edit_inline = false
c.enable_add_inline = false
end
def preconfigure_record_window(c)
super
c.form_config.klass = OrderForm
end
end
Order model的示例窗體。
class OrderForm< Netzke::Basepack::Form
def configure(c)
super
c.model = 'Order'
c.items = [
:description,
:number,
:customer_name
]
end
end
我使用netzke作爲我的表單視圖,在我的控制器中沒有代碼。當我在模型中**提出self.inspect **時,我得到了參數{}中的所有值。我可以給出解決這個問題所需的任何數量的澄清。 – ejo 2013-04-29 07:05:15