Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 生产Rails 3条路线的工作原理与开发不同_Ruby On Rails_Ruby On Rails 3_Routes - Fatal编程技术网

Ruby on rails 生产Rails 3条路线的工作原理与开发不同

Ruby on rails 生产Rails 3条路线的工作原理与开发不同,ruby-on-rails,ruby-on-rails-3,routes,Ruby On Rails,Ruby On Rails 3,Routes,我目前正在尝试让我的Ruby 1.8.7 on Rails 3.0.9应用程序在生产环境中运行。然而,在转移到生产环境时,我的路线遇到了重大问题。例如,在我的代码中,我提到 user_path() 但在制作中,我不得不将其改为 users_path() 这似乎是有效的,除非它是用在 users_path(user) 这将生成URL http://whatwillworkforme.com/users?format=myuser 鉴于发展将产生: http://whatwillworkfo

我目前正在尝试让我的Ruby 1.8.7 on Rails 3.0.9应用程序在生产环境中运行。然而,在转移到生产环境时,我的路线遇到了重大问题。例如,在我的代码中,我提到

user_path()
但在制作中,我不得不将其改为

users_path()
这似乎是有效的,除非它是用在

users_path(user)
这将生成URL

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 实际上是用户的预期行为。您可以通过运行rake路由来验证这一点。你应该从以下几方面入手:

users GET    /users(.:format)        users#index
 user GET    /users/:id(.:format)    users#show
在路由中带有起始冒号的任何内容都将是一个参数,您可以传递给url\u或任何使用它的内容,例如link\u to

您应该使用user_pathuser获取特定用户的路由Rails将在将ActiveRecord实例传递到路由之前将其转换为其id

相反,当您使用复数路由引用整个集合时,应该使用不带参数的users\u path。任何参数都将尝试为url提供格式,例如“xml”应在url中添加一个尾随.xml


在您的特定情况下,还要注意重复的路由:对/用户的POST调用将被Desive拦截,并且不会到达您的控制器创建操作。

user\u路径应该有效。不是吗?在开发中是的,但不是在生产中。这让我感到困惑:你在生产中遇到错误了吗?如果我把用户_路径放回错误中,错误是ActionView::Template::error,没有路由匹配{:action=>show,:controller=>users}:UsersControllers上有一个show方法谢谢。我已经设置了适当的用户路径,用户路径。我正在尝试rake路线,但是在让rake运行时遇到了一些问题,因为它似乎试图在开发模式下而不是在生产模式下做任何事情。必须联系托管公司。试试这个:RAILS_ENV=生产rake routes,或者更好,在您的生产机器上导出RAILS_ENV=生产,以便所有请求都在生产环境中发出。感谢您的建议,我是RailsPlayground的,除非明确说明,否则一切都在开发中。您指定的路由不在我上面包含的rake路由中。抱歉延迟回复,但工作和孩子会浪费很多时间:抱歉,我以为列表是按字母顺序排列的。是的,它包括您指定的路线。我将尝试找出我需要添加到路由中的内容,以使我的代码正常工作,这基本上做到了。我还必须将用户_pathuser更改为用户_pathuser.login。这两种方法在开发中都起作用,但只有后者在生产中起作用。我一直认为RoR比这更容易预测。