2012-04-01 68 views
0

我目前正試圖讓我的Ruby(1.8.7)在Rails(3.0.9)應用程序中運行在生產環境中。但是,在轉移到生產環境中時,我的路線出現重大問題。例如,在我的代碼簡稱生產軌跡3路線與開發工作不一樣

user_path() 

但在生產,我不得不改變,要

users_path() 

這似乎除非它在

users_path(user) 

用於合作哪家產生網址

http://whatwillworkforme.com/users?format=myuser 

開發將產生:

http://whatwillworkforme.com/users/myuser 

我的路線如下所示。我正在使用代碼Smartr項目(https://github.com/dkd/smartr),所以我不熟悉Rails 3路線或爲什麼做出特定決定。

WhatWillWorkForMeCom::Application.routes.draw do 

    devise_for :users, :controllers => { :registrations => "registrations", 
            :passwords  => "passwords", 
            :sessions  => "sessions" } 
    namespace :admin do 
    resources :comments 
    resources :treatments 
    resources :conditions 
    resources :users 
    end 

    match "/conditions/page/:page", :to => "conditions#index" 
    match "/conditions/hot(/:page)", :to => "conditions#hot" 
    match "/conditions/active(/:page)", :to => "conditions#active" 
    match "/conditions/untreated(/:page)", :to => "conditions#untreated" 
    match "/conditions/tagged/:tag(/:page)", :to => "condition#index" 

    match "sitemap/" => "sitemap#index" 

    match 'contact_us' => 'site#contact_us_new', :as => 'contact_us', :via => :get 
    match 'contact_us' => 'site#contact_us_create', :as => 'contact_us', :via => :post 

    resources :conditions, :except => [:show, :edit] do 

    member do 
     put :update_for_toggle_acceptance 
     put :update_for_toggle_decline 
    end 

    collection do 
     get :hot 
     get :active 
     get :untreated 
     get :search 
    end 
    end 

    match "/conditions/:id(/:friendly_id)", :to => "conditions#show", :as => :condition 
    match "/conditions/:id/:friendly_id/edit", :to => "conditions#edit" 
    match "/site/terms_and_conditions", :to => "site#terms_and_conditions" 

    scope "/conditions/:condition_id/:friendly_id/" do 
    get "treatment/:id/edit", :to => "treatments#edit", :as => :edit_condition_treatment 
    put "treatment/:id", :to => "treatments#update", :as => :condition_treatment 
    post "treatments", :to => "treatments#create", :as => :condition_treatments 
    end 

    match "/admin", :to => "admin#index" 

    resources :comments 
    resources :tags, :only => [:index] 
    resources :votes, :only => [:create] 

    resources :users, :except => [:destroy] do 

    collection do 
     get :who_is_online 
     get :search 
    end 

    member do 
     get :reputation 
    end 

    resources :bookmarks, :only => [:index] 

    end 

    resources :bookmarks, :only => [:toggle] do 
    member do 
     post :toggle 
     get :toggle 
    end 
    end 

    match "errors/routing", :to => "errors#routing" 

    namespace :api do 
    namespace :v1 do 
     resources :conditions, :only => [:index] 
     resources :users, :only => [:index] 
    end 
    end 

    root :to => 'site#index' 
    match '*a', :to => 'errors#routing' 
end 

耙路線(如要求)是

 new_user_session GET /users/sign_in(.:format)   {:action=>"new", :controller=>"sessions"} 
      user_session POST /users/sign_in(.:format)   {:action=>"create", :controller=>"sessions"} 
    destroy_user_session GET /users/sign_out(.:format)  {:action=>"destroy", :controller=>"sessions"} 
      user_password POST /users/password(.:format)  {:action=>"create", :controller=>"passwords"} 
     new_user_password GET /users/password/new(.:format) {:action=>"new", :controller=>"passwords"} 
     edit_user_password GET /users/password/edit(.:format) {:action=>"edit", :controller=>"passwords"} 
         PUT /users/password(.:format)  {:action=>"update", :controller=>"passwords"} 
cancel_user_registration GET /users/cancel(.:format)   {:action=>"cancel", :controller=>"registrations"} 
     user_registration POST /users(.:format)     {:action=>"create", :controller=>"registrations"} 
    new_user_registration GET /users/sign_up(.:format)   {:action=>"new", :controller=>"registrations"} 
    edit_user_registration GET /users/edit(.:format)   {:action=>"edit", :controller=>"registrations"} 
         PUT /users(.:format)     {:action=>"update", :controller=>"registrations"} 
         DELETE /users(.:format)     {:action=>"destroy", :controller=>"registrations"} 
     user_confirmation POST /users/confirmation(.:format) {:action=>"create", :controller=>"devise/confirmations"} 
    new_user_confirmation GET /users/confirmation/new(.:format){:action=>"new", :controller=>"devise/confirmations"} 
         GET /users/confirmation(.:format) {:action=>"show", :controller=>"devise/confirmations"} 
who_is_online_users GET /users/who_is_online(.:format)  {:action=>"who_is_online", :controller=>"users"} 
     search_users GET /users/search(.:format)    {:action=>"search", :controller=>"users"} 
    reputation_user GET /users/:id/reputation(.:format)  {:action=>"reputation", :controller=>"users"} 
    user_bookmarks GET /users/:user_id/bookmarks(.:format) {:action=>"index", :controller=>"bookmarks"} 
       users GET /users(.:format)     {:action=>"index", :controller=>"users"} 
        POST /users(.:format)     {:action=>"create", :controller=>"users"} 
      new_user GET /users/new(.:format)    {:action=>"new", :controller=>"users"} 
      edit_user GET /users/:id/edit(.:format)   {:action=>"edit", :controller=>"users"} 
       user GET /users/:id(.:format)    {:action=>"show", :controller=>"users"} 
        PUT /users/:id(.:format)    {:action=>"update", :controller=>"users"} 
+0

'user_path' should work。不是嗎? – 2012-04-01 07:16:04

+0

在開發中是,但沒有在生產中。它讓我困惑:) – user1302719 2012-04-01 07:22:36

+0

你在生產中遇到錯誤嗎? – 2012-04-01 07:22:58

回答

0

我不知道爲什麼開發和生產行爲不同,但http://whatwillworkforme.com/users?format=myuser實際上是對users_path(myuser)預期的行爲。您可以通過運行rake routes進行驗證。你應該得到的東西沿着這些路線:

users GET /users(.:format)  users#index 
user GET /users/:id(.:format) users#show 

任何與在路線起始冒號將可以傳遞給url_for或使用它,如任何一個參數。 link_to

您應該使用user_path(user)來獲取特定用戶的路由(在將路由傳遞到路由之前,Rails會將ActiveRecord實例轉換爲它的id)。

相反,您應該使用users_path而沒有參數,因爲您指的是使用複數路線的整個集合。任何參數都會嘗試爲您的網址提供格式(例如,'xml'應在網址中添加尾隨.xml)。

在您的具體情況下,還需要注意一條重複路線:POST致電/users將被Devise截獲,並且不會觸及您的控制器create的操作。

+0

謝謝你。我已經設置了相應的user_path,users_path。我正在嘗試耙路線,但因爲它似乎試圖在開發模式而不是生產中執行所有操作,因此在運行耙路器時遇到了一些問題。將不得不聯繫託管公司。 – user1302719 2012-04-02 18:44:59

+0

試試這個:'RAILS_ENV = production rake routes',或者更好,在生產機器上執行'export RAILS_ENV = production',這樣所有的請求都是在生產環境中完成的。 – amencarini 2012-04-03 15:19:50

+0

感謝您的建議,我與RailsPlayground,事實證明,除非明確指出,一切都是發展。您指定的路線不在耙路線中(我已經包含在上面)。對不起,在響應延遲,但工作和孩子們殺了很多時間:) – user1302719 2012-04-04 08:12:57