![](/img/trans.png)
[英]Rails 6 ActiveAdmin dynamic dropdown selection with page reload
[英]nested form in Activeadmin with dropdown selection - Ruby on Rails
我有下一個數據庫設計:
一個訂單可以有一個大小 ,一個大小可以有多個訂單 。
例如:
訂單1的大小屬性等於中
訂單2的大小屬性等於中
我希望能夠在活動管理員中創建訂單時選擇尺寸。
訂單型號:
class Order < ApplicationRecord
has_one :order_size
accepts_nested_attributes_for :order_size, :allow_destroy => true
end
訂單大小模型:
class OrderSize < ApplicationRecord
belongs_to :orders
end
另外,OrderSize表必須具有order_id列?
我不知道這些模型是否正確,但是activeadmin給我這個錯誤:
undefined method `order_id' for #<Order:0x007fb980e34b80>
我的activeadmin模型
訂單大小:
ActiveAdmin.register OrderSize do
permit_params :name
end
訂購:
ActiveAdmin.register Order do
permit_params :due_date, :comments, :approved, :order_size_id,
order_size_attributes: [:id, :name,:_destroy]
form do |f|
f.inputs "Admin Details" do
f.input :due_date
f.input :order_size
f.input :comments
f.input :approved
end
f.actions
end
end
我不知道這些模型是否正確,但是activeadmin給我這個錯誤:
訂單:0x007fb980e34b80的未定義方法`order_id'
是的,Rails希望ObjectRelationalMapping(ORM)的關聯模型(訂單)的foreign_key(order_id)
與RDBMS進行通信。 為order_id
創建遷移,然后運行rake db:migrate
解決此問題。
同樣, belongs_to
或has_one
的關聯名稱也應為單數 。
class OrderSize < ApplicationRecord
belongs_to :order
end
此外,您的orders
不應包含order_size_id
,因為關聯為has_one :order_size
。 而是如上所述,您應該在order_sizes
具有order_id
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.