0

So, i have model User

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  attr_accessible :email, :password, :password_confirmation, :remember_me
end

I've created scaffold to that model using rails generators, in controller i added following lines:

class UsersController < ApplicationController
  before_filter :authenticate_user!

  skip_before_filter :require_no_authentication, :only => [:create]

  # ...
end

Finally, when I'm trying to create user I've got this:

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)

So, according to log skip_before_filter didn't work. What am I doing wrong?


EDIT: I don't want give possibility to create user when someone isn't logged in, so skipping :authenticate_user! isn't option. btw. registerable is only temporary in User model.

4

1 回答 1

2

在这种情况下,您需要将更改应用到正确的控制器Devise::RegistrationsController。像这样简单地扩展它(未经测试,但原理应该有效):

# app/controllers/registration_controller.rb
class RegistrationsController < Devise::RegistrationsController
  skip_before_filter :require_no_authentication, :only => [:create]
end

并确保设计使用:

# routes.rb
devise_for :users, :controllers => { :registrations => "registrations" }

您可以在优秀的Devise Wiki中搜索有关扩展 Devise 控制器的许多示例。

于 2012-12-13T22:01:21.803 回答