Can't add sub pages in a controller
Using RoR 2.3.8
This is my cities_controller.rb
class CitiesController < ApplicationController
def show
@city = City.find(params[:id])
...
end
def shops
...
end
def countries
...
end
end
开发者_如何转开发
Here's my routes.rb
map.resources :cities, :collection => {:shops => :get, :countries => :get}
The show
url for each id
is:
http://localhost/cities/1
I want to have some contents shops
and countries
for each associated id
, which I want:
http://localhost/cities/1/shops
http://localhost/cities/1/countries
I can't get the pages showed in empty code in the first place. What have I done wrong?
Thanks.
The :collection
option is for when you want to act on the whole collection - so your routes will show up as:
http://localhost/cities/shops
http://localhost/cities/countries
What you want is
map.resources :cities, :member => {:shops => :get, :countries => :get}
Reference: http://apidock.com/rails/ActionController/Resources/resources
Shops and Countries would probably not be methods in the controller but other models. you would want a Countries.rb
and Shops.rb
You would then nest the resources like
resources :cities do
resources :shops
end
and you would need a belongs_to :city
in the shops model and a has_many :shops
in the cities model which would let you access cities/1/shops.... or something like that
However think about the way the data is structured, do countries really belong to cities (which nesting the resources would imply) or would countries contain cities. You would want cities belongs_to :country
and so on...
This help?
精彩评论