Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails 4中资源路由的作用域_Ruby On Rails_Ruby On Rails 4_Routes_Scope - Fatal编程技术网

Ruby on rails Rails 4中资源路由的作用域

Ruby on rails Rails 4中资源路由的作用域,ruby-on-rails,ruby-on-rails-4,routes,scope,Ruby On Rails,Ruby On Rails 4,Routes,Scope,我有一个resources:shops 这将导致出现/shops、/shops/:id等 我知道我可以用 resources :shops do scope ":city" do # collection and members end end 或者先用它来做 scope ":city" do resources :shops end 但我不知道如何使路由在所有成员(包括标准REST成员)和集合上,就像这样 /shops/:city/ /shops/:city/:id 根

我有一个
resources:shops

这将导致出现
/shops
/shops/:id

我知道我可以用

resources :shops do
  scope ":city" do
   # collection and members
  end
end
或者先用它来做

scope ":city" do
 resources :shops
end
但我不知道如何使路由在所有成员(包括标准REST成员)和集合上,就像这样

/shops/:city/

/shops/:city/:id
根据你的和问题,你试图有逻辑错误的路线。你在城市里有商店,而不是商店里的城市

首先,您应该规范化您的数据库。您应该创建另一个表
cities
,并将
city
属性替换为
shops
表中的
city\u id

你需要
有很多
并且
属于
城市
商店
之间的关联

# Models
class City < ActiveRecord::Base
  has_many :shops
  ... # other stuff
end

class Shop < ActiveRecord::Base
  belongs_to :city
  ... # other stuff
end
它将生成如下路径:

               POST     /cities/:city_id/shops(.:format)          shops#create
new_city_shop  GET      /cities/:city_id/shops/new(.:format)      shops#new
edit_city_shop GET      /cities/:city_id/shops/:id/edit(.:format) shops#edit
    city_shop  GET      /cities/:city_id/shops/:id(.:format)      shops#show
               PATCH    /cities/:city_id/shops/:id(.:format)      shops#update
               PUT      /cities/:city_id/shops/:id(.:format)      shops#update
               DELETE   /cities/:city_id/shops/:id(.:format)      shops#destroy

从逻辑上讲,这些路线将显示特定商店所在的城市。

名称空间

您可能希望考虑包括< 由于您正试图在
城市
中展示商店(即我想象您想在
圣保罗
中展示商店),您可以这样做:

#config/routes.rb
namespace :shops do
   resources :cities, path: "", as: :city, only: [:index] do #-> domain.com/shops/:id/
      resources :shops, path: "", only: [:show] #-> domain.com/shops/:city_id/:id
   end
end
这将允许您创建一个单独的控制器:

#app/controllers/shops/cities_controller.rb
Class Shops::CitiesController < ApplicationController
   def index
      @city = City.find params[:id]
   end
end

#app/controllers/shops/shops_controller.rb
Class Shops::ShopsController < ApplicationController
   def show
      @city = City.find params[:city_id]
      @shop = @city.shops.find params[:id]
   end
end
#app/controller/shops/cities_controller.rb
类商店::CitiesController
这将确保您能够创建所需的路由结构。名称空间做了两件重要的事情-

  • 确保您具有正确的路由结构
  • 分离控制器

  • 你想要像
    /shops/:city/:id
    这样的路线吗?是的,所以每次使用shops路线时,它都会有一个:city范围似乎不标准。你能解释一下你的用例吗?我不介意改变一下。基本上,我有一个城市属性的一堆商店。我只想查看某个城市的商店,并在路线中反映这一点。因此,如果使用友好的_id,/shops/new york/joes晚餐和/shops/los angeles/joes晚餐将导致不同的商店页面。如果需要的话,我不介意创建一个城市模型。这看起来还可以,有没有办法去掉/cities/前缀?为什么要这样做?我不想这样做。。这就是我们的休息路线。。出于安全原因,您可以使用
    友好\u id
    #app/controllers/shops/cities_controller.rb
    Class Shops::CitiesController < ApplicationController
       def index
          @city = City.find params[:id]
       end
    end
    
    #app/controllers/shops/shops_controller.rb
    Class Shops::ShopsController < ApplicationController
       def show
          @city = City.find params[:city_id]
          @shop = @city.shops.find params[:id]
       end
    end