微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决无法在控制器中添加子页面

使用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英镑的城市,依此类推... 有帮助吗?     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。