簡體   English   中英

生產Rails 3路線與開發路線不同

[英]Production Rails 3 Routes not working the same as Development

我目前正在嘗試在生產環境中運行我的Ruby(1.8.7)on Rails(3.0.9)應用程序。 但是,轉移到生產環境后,我的路線遇到了重大問題。 例如我在代碼中提到

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"}

我不確定為什么開發和生產行為會有所不同,但是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帶參數的users_path ,因為您要使用復數路徑來引用整個集合。 任何參數都會嘗試為您的網址提供格式(例如, 'xml'應在網址后添加.xml )。

在您的特定情況下,還要注意重復的路由:對/users POST調用將被Devise攔截,並且不會到達您的控制器create動作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM