无法在控制器中添加子页面

问题描述

使用RoR 2.3.8 这是我的
cities_controller.rb
class CitiesController < ApplicationController
  def show
    @city = City.find(params[:id])
    ...
  end

  def shops
    ...
  end

  def countries
    ...
  end
end
这是我的ѭ2
map.resources :cities,:collection => {:shops => :get,:countries => :get}
每个
id
show
网址为:
http://localhost/cities/1
我想要每个关联的
id
有一些内容
shops
countries
,我想要:
http://localhost/cities/1/shops
http://localhost/cities/1/countries
首先,我无法以空代码显示页面。我做错了什么? 谢谢。     

解决方法

:collection
选项适用于要对整个集合执行操作的情况-这样您的路线将显示为:
http://localhost/cities/shops
http://localhost/cities/countries
你想要的是
map.resources :cities,:member => {:shops => :get,:countries => :get}
参考:http://apidock.com/rails/ActionController/Resources/resources     ,商店和国家/地区可能不是控制器中的方法,而是其他模型中的方法。你想要want14ѭ和
Shops.rb
然后,您将像
resources :cities do
     resources :shops
end
并且您需要在shop模型中的
belongs_to :city
和city模型中的
has_many :shops
,这样您就可以访问城市/ 1 /商店....或类似的东西 但是,考虑一下数据的结构方式,国家是否真正属于城市(这意味着资源的嵌套)或国家包含城市。您可能想要19英镑的城市,依此类推... 有帮助吗?