我正在推出一个包含精选用户组的测试版网站。我想仅在生产环境中禁用注册,并且只在很短的时间内禁用(即我不想完全注册我的注册)。我知道我可以简单地隐藏“注册”链接,但我怀疑黑客比我更聪明,仍然可以使用RESTful路由来完成注册。禁用注册的最佳方法是什么,以便我的测试/开发环境仍然有效,但生产受到影响?谢谢你的任何指示。
我尝试以“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
它传递的是“真实”而非虚假。有没有办法模拟生产环境?我只是在吐这个。
谢谢!
答案 0 :(得分:101)
修改user
模型并移除:registerable
,我认为应该可以提供您想要的内容。
修改强>
我认为这样可行:
if Rails.env.production?
devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable
else
devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable, :registerable
end
答案 1 :(得分:88)
由于其他人遇到了我遇到的问题(请参阅我的评论)。这正是我修复它的方式。我使用了墨菲斯的想法。但是你还需要确保设计使用你的新控制器进行注册路由,否则它对你没什么用。
这是我的控制器覆盖:
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
控制器哈希指定我希望它使用我的重写注册控制器。
无论如何,我希望能节省一些时间。
答案 2 :(得分:11)
仅删除:registerable
无法解决问题。如果您的视图中有一些路线,则会收到错误:
undefined local variable or method 'edit_user_registration_path'
照顾好这一点。
答案 3 :(得分:6)
您可以覆盖Devise :: RegistrationsController和create action以重定向到您想要的页面。控制器应该看起来像这样:
class User::RegistrationsController < Devise::RegistrationsController
def create
redirect_to your_page_path if Rails.env.production?
end
end