ruby-on-rails - 在 Rails 中使用命名空间模型维护 REST

标签 ruby-on-rails rest namespaces

我正在构建一个在线商店,并认为最好为各个复合部件命名。产品、订单、用户等...

我目前的产品模块定义和布局如下

产品::基础 基础产品本身定义尺寸、类型和可用颜色

产品::类型 高级产品分类

产品::颜色 HABTM颜色与Base的关系

产品::单位 可根据不同数量和运费进行扩展

现在我的问题与 Controller 和路由有关。我希望该模块显示为资源,并将其中一个模型(基础)作为“头部”。我已经成功地让它与

一起工作
  scope :module => "product" do
    resources :base, path: "product"
  end

从 rake route 产生

  base_index GET    /product(.:format)          product/base#index
           POST   /product(.:format)          product/base#create
  new_base GET    /product/new(.:format)      product/base#new
 edit_base GET    /product/:id/edit(.:format) product/base#edit
      base GET    /product/:id(.:format)      product/base#show
           PUT    /product/:id(.:format)      product/base#update
           DELETE /product/:id(.:format)      product/base#destroy

这正是我想要的行为,但我的问题是,这种方法是 RESTful 且正确的吗?我宁愿现在被告知我做错了,也不愿进一步了解。 p>

一如既往的感谢。

最佳答案

这是 RESTful,是的,但也试试这个:

Blah::Application.routes.draw do
  namespace :product do
    resource :base
  end  
end

给你:

     product_base POST   /product/base(.:format)      product/bases#create
 new_product_base GET    /product/base/new(.:format)  product/bases#new
edit_product_base GET    /product/base/edit(.:format) product/bases#edit
                  GET    /product/base(.:format)      product/bases#show
                  PUT    /product/base(.:format)      product/bases#update
                  DELETE /product/base(.:format)      product/bases#destroy

关于ruby-on-rails - 在 Rails 中使用命名空间模型维护 REST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16970861/

相关文章:

ruby-on-rails - 关于让 Rails 与 Access 后端一起运行的任何提示?

sql - 如何在 has_many 关系中急切加载最近的对象?

c# - Google OAuth2 token (错误请求)

rest - 通过 jax-rs 返回 XML 对象

javascript - 显示多个API调用的结果

reactjs - 错误 TS2694 : Namespace '"react "' has no exported member ' ReactNode', 'DetailedHTMLProps'、 'HTMLAttributes'、 'SyntheticEvent'、 'HTMLProps'

javascript - 如何拒绝数组中的元素?

ruby-on-rails - 将 curl 调用转换为法拉第请求

python - Python 包中的单类模块

ruby-on-rails-4 - 为什么 Rails 引擎的 gem 名称必须为 "match"模块/命名空间名称?