0
後,我跟隨託尼Amoyal的安裝使用設計/康康舞,只允許管理員創建/註冊新用戶的身份驗證。我沒有完全遵循他,因爲用戶不需要在這個應用程序中擔任多個角色,但是大部分情況下我完全按照他的建議使用。試圖讓admin用戶創建其他用戶,但它一直帶我去編輯用戶提交
它主要工作,但我現在最大的問題是當我嘗試創建一個新用戶並將其提交到註冊表單上時,它立即發出抱怨,將我帶到Devise的編輯註冊表單並抱怨: current_password字段沒有被填寫。如果我填寫任何內容,它將更新我的用戶,而不是我正在嘗試註冊的用戶。
任何幫助,讓它實際上創建用戶,而不是請求更多的變化,將不勝感激。
#控制器/用戶/ registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
before_filter :check_permissions, :only => [:new, :create, :cancel]
skip_before_filter :require_no_authentication
def check_permissions
authorize! :create, resource
end
end
#控制器/ users_controller.rb
class UsersController < ApplicationController
load_and_authorize_resource :except =>[:create]
...
def new
respond_to do |format|
format.json { render :json => @user }
format.xml { render :xml => @user }
format.html
end
end
...
def create
@user = User.new(params[:user])
if @user.save
respond_to do |format|
format.json { render :json => @user.to_json, :status => 200 }
format.xml { head :ok }
format.html { redirect_to :action => :index }
end
else
respond_to do |format|
format.json { render :text => "Could not create user", :status => :unprocessable_entity } # placeholder
format.xml { head :ok }
format.html { render :action => :new, :status => :unprocessable_entity }
end
end
end
end
#視圖/用戶/ new.html.haml
= simple_form_for(@user, :method => :put, :html => { :class=>'form-horizontal' }) do |f|
%fieldset
%legend
= f.input :first_name
= f.input :last_name
= f.input :email
= f.input :password
= f.input :password_confirmation
.form-actions
= f.submit 'Register', :class => 'btn btn-primary'
= link_to 'Back', :back, :class => 'btn'