Rails 3 具有变量命名空间的路由资源

2024-03-23

是否可以有一个变量命名空间?我有如下的宁静资源:

resources :articles
resources :persons

但我需要将它们限定在变量命名空间内,以便它响应以下形式的 URL:

':edition/:controller/:action/:id' 

例如:

/foobar/article/edit/123 or /bazbam/person/edit/345

对于每个资源。这可能吗?resources方法,还是我必须手工制作这些?我不知道可能的值:edition提前时间;这些会在a中查找before_filter in my ApplicationController.

这就是我需要做的全部吗?

scope ':edition' do
  resources :articles
  resources :matches
  resources :teams
end

更新:当使用上面的范围指令时,我得到了我想要的路由:

    articles GET    /:edition/articles(.:format)          {:action=>"index", :controller=>"articles"}
             POST   /:edition/articles(.:format)          {:action=>"create", :controller=>"articles"}
 new_article GET    /:edition/articles/new(.:format)      {:action=>"new", :controller=>"articles"}
edit_article GET    /:edition/articles/:id/edit(.:format) {:action=>"edit", :controller=>"articles"}
     article GET    /:edition/articles/:id(.:format)      {:action=>"show", :controller=>"articles"}
             PUT    /:edition/articles/:id(.:format)      {:action=>"update", :controller=>"articles"}
             DELETE /:edition/articles/:id(.:format)      {:action=>"destroy", :controller=>"articles"}
     matches GET    /:edition/matches(.:format)           {:action=>"index", :controller=>"matches"}
             POST   /:edition/matches(.:format)           {:action=>"create", :controller=>"matches"}
   new_match GET    /:edition/matches/new(.:format)       {:action=>"new", :controller=>"matches"}
  edit_match GET    /:edition/matches/:id/edit(.:format)  {:action=>"edit", :controller=>"matches"}
       match GET    /:edition/matches/:id(.:format)       {:action=>"show", :controller=>"matches"}
             PUT    /:edition/matches/:id(.:format)       {:action=>"update", :controller=>"matches"}
             DELETE /:edition/matches/:id(.:format)       {:action=>"destroy", :controller=>"matches"}
       teams GET    /:edition/teams(.:format)             {:action=>"index", :controller=>"teams"}
             POST   /:edition/teams(.:format)             {:action=>"create", :controller=>"teams"}
    new_team GET    /:edition/teams/new(.:format)         {:action=>"new", :controller=>"teams"}
   edit_team GET    /:edition/teams/:id/edit(.:format)    {:action=>"edit", :controller=>"teams"}
        team GET    /:edition/teams/:id(.:format)         {:action=>"show", :controller=>"teams"}
             PUT    /:edition/teams/:id(.:format)         {:action=>"update", :controller=>"teams"}
             DELETE /:edition/teams/:id(.:format)         {:action=>"destroy", :controller=>"teams"}

我现在可以参考:edition in my ApplicationController:

class ApplicationController < ActionController::Base
  protect_from_forgery

  before_filter :authenticate_user!
  before_filter :get_edition

  def get_edition
    @edition = Edition.first(:conditions => { :FriendlyName => params[:edition] } )
  end

end

现在我只想确保这是实现这一目标的最佳方法。


实际上,您可以执行以下操作:

my_var = "persons"
resources my_var.to_sym

The to_sym字符串上的方法将其更改为符号

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Rails 3 具有变量命名空间的路由资源 的相关文章

  • 有没有办法通过 Outlook API 获取建议的联系人?

    我目前正在开发一个应用程序来获取我的 Microsoft 帐户中的联系人 问题是 与 Google 不同 当我向新联系人发送电子邮件或从新联系人接收电子邮件时 该电子邮件不会复制到 我的联系人 中 因此我无法通过该电子邮件https out
  • 如何根据多对多相关模型的属性查找记录?

    楷模 InternalUser has many internal user roles has many roles through gt internal user roles InternalUserRole belongs to i
  • 用于解析序列化 PHP 数组的 ruby​​ gem? [关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我正在使用 savon v2 的 SOAP Web 服务 并在响应中获取一个我需要解析的序列化 PHP
  • 如何在 Rails 2 中正确使用 OmniAuth

    我正在尝试为我的应用程序使用 OmniAuth 0 2 6 策略 架构为 Rails 2 3 10 Rack 版本为 1 1 OmniAuth 需要此版本或更高版本 出现的问题是 Rails 无法识别我重定向到 auth provider
  • Rails 4/5 发送动态 ActionMailer::Base.mail 电子邮件,附件标记为 Noname

    我看过类似的帖子 主要涉及通过创建视图和控制器来发送附件 例如 电子邮件中的 PDF 附件称为 Noname https stackoverflow com questions 12816042 pdf attachment in emai
  • 广告过滤服务器端[关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我正在开发一个 Web 应用程序 在其中显示来自其他网站的 HTML 在显示最终版本之前 我想去掉广告 关于如何实现这一目标有什么想法
  • Stripe Connect - 检索访问令牌

    我正在尝试为 Rails 3 2 13 应用程序设置 Stripe Connect 我已将用户引导至 Stripe 并收到了来自 Stripe 的授权码 HTTP 1 1 302 Found Location http localhost
  • CORS 与 Amazon S3 和 Cloudfront

    我有一个托管在 Heroku 上的 Rails 应用程序 它使用 CloudFront 以及托管在 S3 上的资产 它完美地显示了资产 尽管需要一些努力 我的 Cloudfront 设置 Forward Headers Whitelist
  • “PG DuplicateTable:错误关系“产品”已存在”- Heroku db:迁移尝试

    Rails 菜鸟尝试将 DB 迁移到 Heroku 请原谅这个错误消息的多汁性 但我认为将其全部包含在内可能很重要 我不知道为什么会发生这种情况 也不知道这意味着什么 我的架构中只有一张产品表和一张创建产品迁移表 Migrating to
  • gem 如何提供 Cucumber 功能和步骤定义?

    我有两个 Rails 项目 A 和 B 其中包含以下宝石 project A gem green theme path gt dev themes green theme gem devel tasks path gt dev themes
  • 在 Ruby 中创建 Microsoft Word (.docx) 文档

    有没有一种简单的方法可以在 Ruby 应用程序中创建 Word 文档 docx 实际上 就我而言 它是一个由 Linux 服务器提供服务的 Rails 应用程序 类似的宝石Prawn http prawn majesticseacreatu
  • 授予 Rails 应用 API 访问权限的最佳身份验证方法

    我想为我的网络应用程序提供经过身份验证的 API 访问 此类服务的消费者通常是其他网站 服务 验证这些用户身份的最佳方法是什么 OAuth openID http 身份验证 正如我们的工作一样 哪个最好 的答案是这样的 是 这取决于 HTT
  • 使用 Rails 自动登录?

    我正在尝试使用 Rails 的 Restful Authentication 插件建立一个简单的身份验证系统 我只是想知道它是如何工作的 b c 我似乎无法弄清楚 cookie 的要求是什么 以及如何做到这一点浏览器始终会记住您 6 个多月
  • 黄瓜和 Rspec

    任何人都可以向我推荐黄瓜和 rspec 教程 rails 3 的好来源 简单示例 吗 Edit 实际上我正在寻找带有很好示例的免费在线资源 我觉得R规格书 http www pragprog com titles achbd the rsp
  • 为什么不重新评估 Binding.scala 路由器?

    我正在尝试通过 Binding scala 为个人项目构建通用路由器 我定义了一个PageState trait sealed trait WhistState def text String def hash String def ren
  • Magento 2 REST API 客户自定义属性

    Magento 2 REST API 文档解释了在更新或创建客户时设置custom attributes 的方法 http devdocs magento com swagger index 20 html http devdocs mag
  • 如何在 Ruby on Rails 中向 select_tag 添加类

    难以为此添加类标签 div class field div 这是我尝试过的 除其他外 div class field div
  • 如何让 Rails 资产管道生成源地图?

    我想让 Rails 与编译后的 CoffeeScript 缩小的 JS 一起生成源映射 以便更好地记录错误 不过 网上似乎还没有关于如何执行此操作的全面文档 有人这样做过吗 我使用 Rails 3 2 和 Heroku Rails 支持缩小
  • Kafka REST 代理 API 有哪些好处?

    我不知道Kafka REST Proxy API的优点 它是一个 REST API 所以我知道它对于管理来说很方便 人们为什么使用 Kafka REST 代理 API 添加对生产者或消费者的 Maven 依赖是否很麻烦 另外 我知道kafk
  • 您将如何整理这个控制器逻辑?

    我在控制器中有一些逻辑 如果满足某些条件 它会设置对象的状态 if params concept consulted legal 0 params concept consulted marketing 1 concept attribut

随机推荐