2011-10-02 91 views
2

我一直在为此奋战数日。我创建了自己的注册控制器,以允许创建和删除用户。我已将:registerable模块留在了我的设计配置中,因为我还希望用户能够编辑其配置文件。我试图把这个模块拿出来看看它是否能解决我的问题。我遇到的问题是,当我创建一个新用户作为管理员时,尽管拥有自己的创建操作,但仍然会将该用户标记为该用户。我尝试了所有我能想到的超越这一点的东西,而且我被卡住了。自定义注册控制器设计不覆盖创建行动

我的注册记忆控制器:

class RegistrationsController < Devise::RegistrationsController 

    load_and_authorize_resource 

    def new 
    super 
    end 

    def create 
    resource.build 
    if resource.save 
     redirect_to users_path 
    else 
     clean_up_passwords(resource) 
     render_with_scope :new 
    end 
    end 

end 

应用控制器:=>注,after_sign_up_path_for在这里重写作为一个测试,看看是否能够工作

class ApplicationController < ActionController::Base 

    protect_from_forgery 

    rescue_from CanCan::AccessDenied do |exception| 
    flash[:error] = exception.message 
    redirect_to projects_url 
    end 

    protected 

    def stored_location_for(resource) 
     nil 
    end 

    def after_sign_in_path_for(resource) 
     projects_url 
    end 

    def after_sign_up_path_for(resource) 
     users_path 
    end 

end 

routes文件:

DeviseTest::Application.routes.draw do 

    devise_for :users, :controller => { :registrations => "registrations"} 
    devise_scope :user do 
    get '/login' => 'devise/sessions#new' 
    get '/logout' => 'devise/sessions#destroy' 
    end 

    resources :users, :controller => "users" 

    resources :projects 

    root :to => 'home#index' 

    end 

和我的用户管理员视图的控制器

class UsersController < ApplicationController 

    load_and_authorize_resource 

    # GET /users 
    # GET /users.xml 
    def index 
    @users = User.excludes(:id => current_user.id) 

    respond_to do |format| 
     format.html # index.html.erb 
     format.xml { render :xml => @users } 
    end 
    end 

    # DELETE /users/1 
    # DELETE /users/1.xml 
    def destroy 
    @user = User.find(params[:id]) 
    @user.destroy 

    respond_to do |format| 
     format.html { redirect_to(users_url) } 
     format.xml { head :ok } 
    end 
    end 
end 

耙路线输出:如预期

new_user_session GET /users/sign_in(.:format)  {:action=>"new", :controller=>"devise/sessions"} 
      user_session POST /users/sign_in(.:format)  {:action=>"create", :controller=>"devise/sessions"} 
    destroy_user_session DELETE /users/sign_out(.:format)  {:action=>"destroy", :controller=>"devise/sessions"} 
      user_password POST /users/password(.:format)  {:action=>"create", :controller=>"devise/passwords"} 
     new_user_password GET /users/password/new(.:format) {:action=>"new", :controller=>"devise/passwords"} 
     edit_user_password GET /users/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"} 
         PUT /users/password(.:format)  {:action=>"update", :controller=>"devise/passwords"} 
cancel_user_registration GET /users/cancel(.:format)  {:action=>"cancel", :controller=>"devise/registrations"} 
     user_registration POST /users(.:format)    {:action=>"create", :controller=>"devise/registrations"} 
    new_user_registration GET /users/sign_up(.:format)  {:action=>"new", :controller=>"devise/registrations"} 
    edit_user_registration GET /users/edit(.:format)   {:action=>"edit", :controller=>"devise/registrations"} 
         PUT /users(.:format)    {:action=>"update", :controller=>"devise/registrations"} 
         DELETE /users(.:format)    {:action=>"destroy", :controller=>"devise/registrations"} 
        login GET /login(.:format)    {:controller=>"devise/sessions", :action=>"new"} 
        logout GET /logout(.:format)    {:controller=>"devise/sessions", :action=>"destroy"} 
        users GET /users(.:format)    {:action=>"index", :controller=>"users"} 
         POST /users(.:format)    {:action=>"create", :controller=>"users"} 
       new_user GET /users/new(.:format)   {:action=>"new", :controller=>"users"} 
       edit_user GET /users/:id/edit(.:format)  {:action=>"edit", :controller=>"users"} 
        user GET /users/:id(.:format)   {:action=>"show", :controller=>"users"} 
         PUT /users/:id(.:format)   {:action=>"update", :controller=>"users"} 
         DELETE /users/:id(.:format)   {:action=>"destroy", :controller=>"users"} 
       projects GET /projects(.:format)   {:action=>"index", :controller=>"projects"} 
         POST /projects(.:format)   {:action=>"create", :controller=>"projects"} 
      new_project GET /projects/new(.:format)  {:action=>"new", :controller=>"projects"} 
      edit_project GET /projects/:id/edit(.:format) {:action=>"edit", :controller=>"projects"} 
       project GET /projects/:id(.:format)  {:action=>"show", :controller=>"projects"} 
         PUT /projects/:id(.:format)  {:action=>"update", :controller=>"projects"} 
         DELETE /projects/:id(.:format)  {:action=>"destroy", :controller=>"projects"} 
        root  /(.:format)     {:controller=>"home", :action=>"index"} 

其他一切作品,我只是不能创建的用户没有签署这不是一个巨大的问题创建一个用户,但如果我需要的。要创建3个或4个,每次登录,签名都是一个巨大的皮塔饼。

对此非常感谢。

+0

什么'rake routes'输出? – cbrauchli

+0

添加了耙路输出。 – janders223

回答

3

在你的routes.rb文件的第三行,我想你的意思:controllers => …,不:controller => …。你错过了's'。

+0

我一定忽略了那百倍。只是证明有时需要额外的眼睛。谢谢。 – janders223