所以,我有模特用户
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me
end
我使用rails生成器为该模型创建了脚手架,在控制器中添加了以下行:
class UsersController < ApplicationController
before_filter :authenticate_user!
skip_before_filter :require_no_authentication, :only => [:create]
# ...
end
最后,当我尝试创建用户时,我得到了这个:
Started POST "/users" for 127.0.0.1 at 2012-12-13 22:33:34 +0100
Processing by Devise::RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"wkJ8ZxVUg9eaFiL+Guu+QIfjGiwGANReiv2bTu3YQPg=", "user"=>{"email"=>"test@test.pl", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Save"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Redirected to http://localhost:3000/
Filter chain halted as :require_no_authentication rendered or redirected
Completed 302 Found in 2ms (ActiveRecord: 0.3ms)
所以,根据日志skip_before_filter没有用。我做错了什么?
编辑:当有人没有登录时,我不希望有可能创建用户,所以跳过:authenticate_user!不是选择。顺便说一句。可注册只是用户模型中的暂时性。
答案 0 :(得分:2)
您需要将更改应用于正确的控制器,在本例中为Devise::RegistrationsController
。只需像这样扩展(未经测试,但原则应该有效):
# app/controllers/registration_controller.rb
class RegistrationsController < Devise::RegistrationsController
skip_before_filter :require_no_authentication, :only => [:create]
end
并确保Devise使用:
# routes.rb
devise_for :users, :controllers => { :registrations => "registrations" }
您可以搜索优秀的Devise Wiki以获取有关扩展Devise控制器的许多示例。