6

Spacesバー、レストランなど、さまざまなタイプの場所を持つモデルがあります。同じ列、同じ、モデル、コントローラーなどがあります。派手な STI はありません。Space_type必要なフィールドが 1 つだけあります。エイリアスルートを決定します。

代わりに domain.com/spaces/12345/bars/12345または/clubs/12345

現在私は持っています:

  resources :spaces do
    collection do
      get :update_availables
      get :update_search
      get :autocomplete
    end
    member do
      post :publish
      post :scrape
    end
    resources :photos do
      collection do
        put :sort
      end
    end

    resources :reviews
  end

また、これを行う方法はありますか?

4

2 に答える 2

6

The routes are not a way to interact with your model directly. So, as long as you write a standard route, you can make things work. For instance, to make /bars/12345 and /clubs/12345 for your spaces_controller (or whatever the name of the controller is) , you can create routes like :

scope :path => '/bars', :controller => :spaces do
  get '/:id' => :show_bars, :as => 'bar'
end  

scope :path => '/clubs', :controller => :spaces do
  get '/:id' => :show_clubs, :as => 'clubs'
end  
于 2011-04-15T09:35:54.617 に答える
5
# routes.rb
match "/:space_type/:id", :to => "spaces#show", :as => :space_type

# linking
link_to "My space", space_type_path(@space.space_type, @space.id)

which will generate this urls: /bars/123, /clubs/1 ... any space_type you have

And it looks like STI wold do this job little cleaner ;)

UPD

Also you can add constraints to prevent some collisions:

match "/:space_type/:id", :to => "spaces#show", :as => :space_type, :constraints => { :space_type => /bars|clubs|hotels/ }

And yes - it is good idea to put this rout in the bottom of all other routes

You can also wrap it as a helper (and rewrite your default space_url):

module SpacesHelper
  def mod_space_url(space, *attrs)
    # I don't know if you need to pluralize your space_type: space.space_type.pluralize
    space_type_url(space.space_type, space.id, attrs)
  end
end
于 2011-04-15T09:36:16.623 に答える