仅对生产环境禁用Devise注册

我正在启动一个testing版网站,其中包括一组用户。 我只想在生产环境中禁用注册,并且只在很短的时间内(即我不想完全注册我的注册)。 我知道我可以简单地隐藏“注册”链接,但是我怀疑黑客比我还可以使用RESTful路由来完成注册。 什么是禁用注册的最好方法,所以我的testing/开发环境仍然有效,但是生产受到影响? 感谢任何指针。

我已经试过用“sign_up”指向“sign_in”的方式来指定命名范围,但是它不起作用。 以下是我所尝试的:

devise_scope :user do get "users/sign_in", :to => "devise/sessions#new", :as => :sign_in get "users/sign_up", :to => "devise/sessions#new", :as => :sign_up end 

理想情况下,我们会将用户发送到“pages#registration_disabled”页面或类似的东西。 我只是想得到一些我可以玩的东西。

编辑:我已经按要求更改模型,然后将以下内容添加到/spec/user_spec.rb

 describe "validations" do it "should fail registration if in production mode" do ENV['RAILS_ENV'] = "production" @user = Factory(:user).should_not be_valid end end 

它传递的是“真实的”而不是假的。 有没有办法模拟生产环境? 我只是吐这个。

谢谢!

由于其他人有我遇到的问题(请参阅我的意见)。 这正是我如何解决它。 我用murphyslaw的想法。 但是,您还需要确保devise使用您的新控制器注册路由,否则它不会为您做很多。

这是我的控制器覆盖:

 class RegistrationsController < Devise::RegistrationsController def new flash[:info] = 'Registrations are not open yet, but please check back soon' redirect_to root_path end def create flash[:info] = 'Registrations are not open yet, but please check back soon' redirect_to root_path end end 

我已经添加了Flash消息,告诉任何人在注册页面上绊倒了为什么它不工作。

这是什么在我的routes.rb

  if Rails.env.production? devise_for :users, :controllers => { :registrations => "registrations" } else devise_for :users end 

控制器散列指定我希望它使用我的重写注册控制器。

无论如何,我希望能省一些时间。

编辑user模型并删除:registerable ,我认为应该给你你想要的。

编辑:

我认为这将工作:

 if Rails.env.production? devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable else devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable, :registerable end 

只有删除:registerable不会解决问题。 如果你在视图中有一些路线,你会得到一个错误:

undefined local variable or method 'edit_user_registration_path'

照顾这个。

你可以重写Devise :: RegistrationsController和create action来redirect到你想要的页面。 控制器应该看起来像这样:

 class User::RegistrationsController < Devise::RegistrationsController def create redirect_to your_page_path if Rails.env.production? end end