1

STIを使用したサブクラスを持つモデルプロパティがあります。

サブクラスに応じて異なるビューパーシャルのみで同じコントローラーを使用したいと思います。

Property
Restaurant < Property
Landmark < Property

コントローラー内のサブクラスを識別して正しいビューをレンダリングする方法がわからない場合を除いて、検出されます。つまり。/ restaurantsは機能し、プロパティコントローラーに移動しますが、Restaurantサブクラスが必要かどうかわかりません。

map.resources :restaurant, :controller => :properties
map.resources :properties
4

1 に答える 1

5

問題を解決する簡単な方法は、サブコントローラーを作成することです。

class RestaurantsController < PropertiesController
end

ルートでは、レストランをレストランコントローラーにマップします。

更新:あるいは、あなたのroutes.rb:でこのようなことを試すことができます

map.resources :restaurants, :controller => :properties, :requirements => {:what => :Restaurant}
map.resources :properties, :requirements => {:what => :Property}

次に、beforeフィルターを使用してparams [:what]をチェックし、それに応じて動作を変更できます。

例:

class PropertiesController < ApplicationController
  before_filter select_model

  def select_model
    @model = params[:what].constantize
  end

  def show
    @model.find(params[:id])
    ...
  end

  ...
end
于 2010-02-16T23:53:21.323 に答える