2

我有setup my versioned API这样,只有一小部分向后兼容性。在我的路线,我有:Rails 3使用可选范围内的资源进行路由

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

已达成具有以下网址的触角一样的地方

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

我的目录结构是这样的成功目标:

enter image description here

我看到的问题是在我的观点中,我的链接世代。例如,声音展示页面上我有一个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和具有rails.js的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和其他视图标签,以使url助手为我的设置生成正确的路径?“我已经尝试了几乎所有我能想到的组合,但不能让它们在正确的位置结束。

+0

您是否使用jQuery或Prototype作为JavaScript框架。如果是这样的话,它包含在给你问题的页面上?另外,您确定您的rails.js文件与您的JavaScript框架相匹配吗?我想这个问题是在JavaScript的某个地方。但是show操作正在处理POST请求也很奇怪。 – aNoble 2011-05-07 06:27:59

+0

我正在使用jQuery 1.4.4以及jquery的最新rails.js。我更新了我的问题,以表明JS的头脑 – coneybeare 2011-05-07 14:07:05

回答

2

原来这个解决方案很简单,我有两个错误,导致错误的路由。生成和URL追加PARAMS

  1. resources :sounds路线上面,我误了这条线。

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

    我此行,sounds/123/xml可以缓存页面这导致所有的路由到show,我意识到错误的是我有:format在parens,比赛应该是get。现在,有这么一句话:

    get '/sounds/:id/:format' => 'sounds#show' 
    
  2. 接下来,在button_to链接,我放置@sound对象作为我的第二个PARAM。 Rails试图通过推断正确的URL来进行智能化,但是失败的是可选参数:version param。将其更改为

    sound_path(:id => @sound.id) 
    

    像一个魅力工作。