このシナリオにアプローチする最善の方法は不明であり、ある方向に感謝します。基本的に私は、組織の資産を記録する必要があるシナリオを持っています。多態性、STIなど
location
make
model
colour
purchase_date
warranty_period
同様に: How to design a product table for many kinds of product where each product has many parameters
私が持っていた属性が入力するタイプと異なるよう
は、しかし、のようなすべての資産に共通するフィールドの数がある資産の様々な種類があります。
one-to_many between Organisation and Asset
polymorhpic between Asset and Details
class Organisation < ApplicationRecord
has_many :assets
end
class Asset < ApplicationRecord
belongs_to :organisation
belongs to :detail, polymorphic: true
end
class CarAsset < ApplicationRecord
has_one :asset, as: :detail
end
class ComputerAsset < ApplicationRecord
has_one :asset, as: :detail
end
としてこれを作成するを通じて私の質問は: 資産タイプ&を単一のフォームアクションで作成したいので、ユーザーが資産タイプを選択した後、両方のモデルに対して単一のフォームエントリを作成します。
、ユーザーが組織のショーのページのリンクをクリックします:私も値をチェックすることができ、私の見解では
class AssetsController < ApplicationController
def new
@organisation = Organisation.find(params["organisation_id"])
@asset = @organisation.assets.new
case params[:query]
when "new_car_asset"
@details = @asset.car_assets.new
when "new_computer_asset"
@details = @asset.computer_assets.new
end
end
end
:私のコントローラで
<%= link_to "New car asset", new_organisation_asset_path(@organisation, query: :new_car_asset) %>
ザ・を私は次のように何かを行うことができますparams [:query]を呼び出して、アセットタイプに関連する対応するフォームパーシャルをレンダリングします。
これは正しいパスになるか、これを実現する良い方法がありますか?それは非常にclunky感じる。