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) 
    

    像一個魅力工作。