【问题标题】:Rails 3 routing with resources under an optional scopeRails 3 在可选范围内使用资源进行路由
【发布时间】:2011-08-20 14:36:32
【问题描述】:

我有这样的setup my versioned API,只是为了向后兼容做了一个小的调整。在我的路线中,我有:

scope '(api(/:version))', :module => :api, :version => /v\d+?/ do
    …
    scope '(categories/:category_id)', :category_id => /\d+/ do
        …
        resources :sounds
        …
    end
end

已经实现了让以下 URL 到达同一个地方的成功目标

/api/v1/categories/1/sounds/2
/api/categories/1/sounds/2
/categories/1/sounds/2
/sounds/2

我的目录结构是这样的:

在我看来,我看到的问题在于我的链接生成。例如,在声音显示页面我有一个button_to 来删除声音

<%= button_to 'Delete', @sound, :confirm => 'Are you sure?', :method => :delete %>

它以action 的形式生成以下网址:

"/api/sounds/1371?version=1371"

此外,delete 方法不起作用,而是作为 POST 发送

rake routes 有趣的部分是:

                     sounds GET    (/api(/:version))(/categories/:category_id)/sounds(.:format)                               {:controller=>"api/sounds", :version=>/v\d+?/, :action=>"index", :category_id=>/\d+/}
                            POST   (/api(/:version))(/categories/:category_id)/sounds(.:format)                               {:controller=>"api/sounds", :version=>/v\d+?/, :action=>"create", :category_id=>/\d+/}
                  new_sound GET    (/api(/:version))(/categories/:category_id)/sounds/new(.:format)                           {:controller=>"api/sounds", :version=>/v\d+?/, :action=>"new", :category_id=>/\d+/}
                 edit_sound GET    (/api(/:version))(/categories/:category_id)/sounds/:id/edit(.:format)                      {:controller=>"api/sounds", :version=>/v\d+?/, :action=>"edit", :category_id=>/\d+/}
                      sound GET    (/api(/:version))(/categories/:category_id)/sounds/:id(.:format)                           {:controller=>"api/sounds", :version=>/v\d+?/, :action=>"show", :category_id=>/\d+/}
                            PUT    (/api(/:version))(/categories/:category_id)/sounds/:id(.:format)                           {:controller=>"api/sounds", :version=>/v\d+?/, :action=>"update", :category_id=>/\d+/}
                            DELETE (/api(/:version))(/categories/:category_id)/sounds/:id(.:format)                           {:controller=>"api/sounds", :version=>/v\d+?/, :action=>"destroy", :category_id=>/\d+/}

服务器日志显示:

Started POST "/api/sounds/1371?version=1371" for 127.0.0.1 at Fri May 06 23:28:27 -0400 2011
  Processing by Api::SoundsController#show as HTML
  Parameters: {"authenticity_token"=>"W+QlCKjONG5i/buIgLqsrm3IHi5gdQVzFGYGREpmWYs=", "id"=>"1371", "version"=>371}

我使用 JQuery 作为我的 UJS,并且拥有用于 JQuery 的最新版本 rails.js:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script> <!-- must be >= 1.4.4 for the rails 3 link js to work—> 
<script src="/javascripts/jquery-ujs/src/rails.js?1304736923" type="text/javascript"></script>




我知道这是tl;dr,但我的问题是:“我需要在我的 button_to link_to 和其他视图标签中添加什么,以使 url 帮助程序为我的设置生成正确的路径?”我有尝试了几乎所有我能想到的组合,但始终无法让它们最终出现在正确的位置。

【问题讨论】:

  • 您是在使用 jQuery 还是 Prototype 作为 JavaScript 框架。如果是这样,它是否包含在给您带来问题的页面中?另外,您确定您的 rails.js 文件与您的 JavaScript 框架匹配吗?我猜想问题出在 JavaScript 的某个地方。但同样奇怪的是 show 动作正在处理 POST 请求。
  • 我使用的是 jQuery 1.4.4,以及最新的用于 jquery 的 rails.js。我更新了我的问题以在头部显示 JS

标签: ruby-on-rails ruby-on-rails-3 routes nested-routes custom-routes


【解决方案1】:

结果证明解决方案很简单。我这里有两个错误导致生成错误的路线和附加参数的url。

  1. resources :sounds 路线上方,我错误地有这一行:

    match '/sounds/:id(/:format)' => 'sounds#show'
    

    我有这一行,以便sounds/123/xml 可以被页面缓存。这导致所有路由到show,我意识到错误是我在括号中有:format,匹配应该是get。现在是这样的:

    get '/sounds/:id/:format' => 'sounds#show'
    
  2. 接下来,在button_to 链接中,我将@sound 对象作为我的第二个参数。 Rails 试图智能地从中推断出正确的 url,但由于可选的 api :version 参数而失败。将其更改为

    sound_path(:id => @sound.id)
    

    工作就像一个魅力。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-25
    • 2022-01-17
    • 1970-01-01
    • 2011-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多