2012-04-24 32 views
0

我想標題是自我解釋的......我如何僅通過身份驗證的用戶使用設計創建用戶?
當我嘗試添加一個新的(通過身份驗證時),設計說,我alreade身份驗證
任何解決方法?
非常感謝如何啓用只有經過身份驗證的用戶才能創建新用戶

編輯:根據要求,我的代碼

用戶表單

<% if @user == current_user %> 
<i class="icon-info-sign"> </i><i>Após alterar seus dados, você terá de fazer login novamente</i> 
<% end %> 

<%= form_for(@user, :html => { :class => "well form-horizontal"}) do |f| %> 
    <% if @user.errors.any? %> 
    <div class="alert alert-error"> 
     <h2><%= pluralize(@user.errors.count, "erro") %> 
     <% if @user.errors.count == 1 %> impede 
      <% else %> 
      impedem 
     <% end %> 
     a continuação:</h2> 

     <ul> 
     <% @user.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 


<label>Nome</label><%= f.text_field :nome %> 
<label>E-mail</label><%= f.email_field :email %> 

<% if @user == current_user or @user.created_at == nil%> 
    <label>Senha</label><%= f.password_field :password %> 
<% end %> 

<div> 
<br /> 
<button class="btn" type="submit"> 
    Salvar alterações 
</button> 

</div> 

<% end %> 

用戶控制器的一部分

before_filter :authenticate_user! 
    def index 
    @users = User.all 

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

    # GET /users/1 
    # GET /users/1.json 
    def show 
    @user = User.find(params[:id]) 

    respond_to do |format| 
     format.html # show.html.erb 
     format.json { render json: @user } 
    end 
    end 

    # GET /users/new 
    # GET /users/new.json 
    def new 
    @user = User.new 

    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @user } 
    end 
    end 

    # GET /users/1/edit 
    def edit 
    @user = User.find(params[:id]) 
    end 

    # POST /users 
    # POST /users.json 
    def create 
    @user = User.new(params[:user]) 

    respond_to do |format| 
     if @user.save 
     format.html { redirect_to @user, notice: 'User was successfully created.' } 
     format.json { render json: @user, status: :created, location: @user } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @user.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PUT /users/1 
    # PUT /users/1.json 
    def update 
    @user = User.find(params[:id]) 

    respond_to do |format| 
     if @user.update_attributes(params[:user]) 
     format.html { redirect_to @user, notice: 'User was successfully updated.' } 
     format.json { head :no_content } 
     else 
     format.html { render action: "edit" } 
     format.json { render json: @user.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

應用控制器

class ApplicationController < ActionController::Base 
    protect_from_forgery 

    def after_sign_in_path_for(resource) 
     return request.env['omniauth.origin'] || stored_location_for(resource) || painel_path 
    end 
end 

路線的一部分

應用:: Application.routes.draw做

resources :acampantes 

    devise_for :users, :skip => [:registrations] 

    resources :profiles 

    resources :eventos do 
    resources :acampantes 
    end 

    # aqui tem um has_many, mas não precisa de nested routes 
    resources :noticias 
    resources :users#, :as => "usuarios" 
+1

你編碼了什麼嗎?告訴我們你的嘗試。 – 2012-04-25 00:26:47

+0

好吧......我剛剛在我的用戶模型上安裝了設計,並生成了設計視圖...並沒有改變任何東西。這裏是我的'用戶'類:https://gist.github.com/2484961編輯:當我嘗試通過'/ users/sign_up'添加一個新用戶時,我得到'您已經登錄'的消息。當'/ users/new'加入同樣的信息時 – 2012-04-25 00:44:42

回答

1

做到這一點。將覆蓋registrations_controller.rb理想的方式(如所示here)和跳過newcreate操作的before_filter檢查。我試圖自己做,但不幸的是,我無法做到。

另一種方式來做到這一點,這工作對我來說,將是由設計所提供的註冊記憶控制器預留創建一個單獨的用戶控制器:

class UsersController < ApplicationController 
    before_filter :authenticate_user! 

    def new 
    @user = User.new 
    end 

    def create 
    @user = User.new(params[:user]) 
    if @user.save 
     flash[:success] = "User saved" 
     redirect_to @user 
    else 
     render 'new' 
    end 
    end 
end 

# routes.rb 
ApplicationName::Application.routes.draw do 
    devise_for :users 
    resources :users, :only => [:new, :create] 
end 

然後做一個形式新動作:

# app/views/users/new.html.erb 
<%= form_for(@user) do |f| %> 

    <div><%= f.label :email %><br /> 
    <%= f.email_field :email %></div> 

    <div><%= f.label :password %><br /> 
    <%= f.password_field :password %></div> 

    <div><%= f.label :password_confirmation %><br /> 
    <%= f.password_field :password_confirmation %></div> 

    <div><%= f.submit "Sign up" %></div> 
<% end %> 

這種方法的問題在於由Devise提供的用於註冊的路線仍然存在。解決方案是防止Devise生成涉及註冊的路由:

# routes.rb 
devise_for :users, :skip => [:registrations] 

這意味着您將完全忽略Registrations模塊並創建自己的。我從Bare-bone, stripped-down Devise tutorial得到了這個想法。請注意,您應該使控制器的操作和視圖取代由Devise的註冊模塊提供的操作和視圖。這包括new,edit,create,updatedestroy

+0

剛剛添加了':skip => [:registrations]''。我已經有了新用戶的形式。非常感謝 – 2012-04-25 15:53:32

+0

確定,**幾乎**一切正常......在創建一個新的'User'後,我被'踢出'了我的應用程序。有任何想法嗎? – 2012-04-25 23:29:52

+0

「踢出」是什麼意思?你是否被應用程序註銷或什麼? – 2012-04-26 01:01:59

相關問題