Different routes but using the same controller for model subclasses in Rails

一曲冷凌霜 提交于 2019-12-06 15:22:55
Tomas Markauskas

A simple way to fi the problem would be to create a Sub-Controller:

class RestaurantsController < PropertiesController
end

In the routes you would map restaurants to the restaurants controller.

Update: Alternatively you could try something like this in your routes.rb:

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

Then you can use a before filter to check params[:what] and change behaviour accordingly.

Example:

class PropertiesController < ApplicationController
  before_filter select_model

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

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

  ...
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!