0

だから、私はモデルユーザーを持っています

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

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

レールジェネレーターを使用してそのモデルに足場を作成しました。コントローラーに次の行を追加しました。

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をスキップしてください!オプションではありません。ところで。登録可能は、ユーザーモデルでは一時的なものです。

4

1 に答える 1

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のコントローラーの拡張に関する多くの例については、優れたDeviseWikiを検索できます。

于 2012-12-13T22:01:21.803 に答える