在可选范围内使用资源进行Rails 3路由 [英] Rails 3 routing with resources under an optional scope

查看:81
本文介绍了在可选范围内使用资源进行Rails 3路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了我的API版本只需进行少量调整即可向后兼容。在我的路线中,我有:

I have setup my versioned API like this with only a small tweak for backwards compatibility. In my routes I have:

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

已达到成功目标,即使以下URL到达相同位置

with the successful goal already reached of having the following URL's reach the same place

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

我的目录结构是这样的:

My directory structure is like this:

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

The problem I am seeing is in my link generations in my views. For example, on the sound show page I have a button_to to delete the sound

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

哪个会以 action :

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

此外,删除方法不起作用,而是作为<$ c发送$ c> POST

And furthermore, the delete method is not working, instead it is being sent as a POST

耙路的有趣部分是:

                     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,并且具有最新版本的rails.js jQuery:

I am using JQuery as my UJS and have the most recent version of rails.js for JQuery:

<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和其他视图标记中添加什么才能制作网址助手会为我的设置生成正确的路径?我已经尝试了几乎所有我能想到的组合,但始终无法使它们以正确的位置结束。




I know this is tl;dr but my question is: "What do I need to put in my button_to link_to and other view tags to make the url helpers generate the correct path for my setup?" I have tried just about every combination I can think of but can never get them to end up in the right place.

推荐答案

原来,该解决方案很简单。我在这里遇到了两个错误,导致生成错误的路线以及添加参数的网址。

Turns out the solution was an easy one. I had two mistakes here that caused the wrong route to generate and the url to append params.


  1. 在<$ c $上方c> resources:sounds 路线,我错误地输入了这一行:

  1. above the resources :sounds route, I mistakenly had this line:

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

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

I have this line so that sounds/123/xml can be page cached. This caused all the routing to show and I realized the mistake was that I had the :format in parens, and the match should be a get. Now it reads:

get '/sounds/:id/:format' => 'sounds#show'


  • 接下来,在 button_to 链接,我将 @sound 对象作为第二个参数。 Rails试图从中推断出正确的URL,但尝试使用api :version 参数失败。更改为

  • Next, in the button_to link, I was placing the @sound object as my second param. Rails was trying to be intelligent an extrapolate the right url from this, but was failing with the optional api :version param. Changing it to

    sound_path(:id => @sound.id)
    

    像护身符一样工作。

    这篇关于在可选范围内使用资源进行Rails 3路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆