2017-01-18 12 views
0

私の既存のアプリは、ユーザ認証にDeviseを使用しています。これは、ユーザ登録/確認プロセスでうまく動作します。ユーザーは電子メールアドレス、パスワード、およびパスワード確認を入力するだけです。彼らは確認メールを受け取って、すべてが良いです!Ruby on Railsのパスワードオーバーライドを変更する

ここで、account_adminsに、追加のユーザーを作成できるようにしたいと考えています。

コントローラとフォームが接続されており、ユーザーが作成されています。しかし、account_adminがpasswordpassword_confirmationフィールドをバイパスできるようにする必要があることが私には分かります。 それ以外の場合は、account_adminユーザーが作成したパスワードで各ユーザーに個別の電子メールを送信する必要があります。

代わりに、それだけでユーザーを作成し、ユーザに&電子メールにパスワードを考案を作成している、account_adminが必要なフィールドfirst_nameに記入していlast_namephone_number、およびemailする方が理にかなっています。私はDevise resourcesかなりの数を見てきましたし、other things皆さんが行っているが、それらのほとんどは完全に私がやりたくない、などよくご確認のプロセスを再配線私を持っている。

<%= form_for @user, url: users_admin_index_path(@user) do |f| %> 
    <%= f.label :first_name, "First Name", class: "control-label col-md-2 col-sm-2 col-xs-12" %> 
    <%= f.text_field :first_name, class: "form-control", :required => true, required: "" %> 

    <%= f.label :last_name, "Last Name", class: "control-label col-md-2 col-sm-2 col-xs-12" %> 
    <%= f.text_field :last_name, class: "form-control", :required => true, required: "" %> 

    <%= f.label :email, "Email", class: "control-label col-md-2 col-sm-2 col-xs-12" %> 
    <%= f.email_field :email, class: "form-control", :required => true, required: "" %> 

    <%= f.label :phone_number, "Phone Number", class: "control-label col-md-2 col-sm-2 col-xs-12" %> 
    <%= f.text_field :phone_number, class: "form-control", :required => true, required: "" %> 

    <%= f.label :password, "Password", class: "control-label col-md-2 col-sm-2 col-xs-12" %> 
    <%= f.password_field :password, class: "form-control", :required => true, required: "" %> 

    <%= f.label :password_confirmation, "Password Confirmation", class: "control-label col-md-2 col-sm-2 col-xs-12" %> 
    <%= f.password_field :password_confirmation, class: "form-control", :required => true, required: "" %> 

    <%= link_to "Cancel", "", class: "btn btn-danger" %> 
    <%= f.submit "Submit", class: "btn btn-success" %> 
<% end %> 

と関連するコントローラメソッドcontrollers/users_controller.rb

だから、ここに私のフォームビューviews/users/new.html.erbある

class UsersController < ApplicationController 
    before_action :get_company_and_locations 

    def new 
    if current_user.is_account_owner 
     @user = User.new 
    else 
     flash[:danger] = "You do not have permission to do this action!" 
    end 
    end 

    def create 
    @user = User.new(user_params) 
    @user.company_id = current_user.company.id 
    if @user.save 
     flash[:success] = "User succesfully created!" 
     redirect_to :back 
    else 
     render :new 
    end 
    end 



    private 

    def user_params 
    params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation) 
    end 
end 

そして、私はまた私のUserモデルの工夫使用していているので、私がする必要私のroutes.rbに以下を追加してください:

resources :users_admin, :controller => 'users' 

答えて

0

あなたはdevise_invitableの使用を検討しましたか?

https://github.com/scambra/devise_invitable

これは、あなたの新しいユーザーに招待状を送って、あなたは余分なユーザーデータを作成するために、フォームを使用させます - 宝石自体はただサイトへのバックリンクを持ってスタブのメールを持っていますパスワードを設定できるトークンで

これは複雑な処理ではありません。

1は

gem 'devise_invitable' 

2ランは、初期化子

rails generate devise_invitable:install 

3ランモデル

rails generate devise_invitable User 
用発電機のための発電機をインストールするインストールし、あなたのGemfileに宝石を追加し、バンドル実行します

4モデルに招待状を追加します

# your list of mods might be different, it will depend on what you're using 
    devise :database_authenticatable, :registerable, :omniauthable, :recoverable, :rememberable, :trackable , :validatable , :confirmable, :invitable 

5あなたの招待ページ

rails generate devise_invitable:views 
を編集できるようにビューを生成
rails g migration add_invitable_to_user 

オープン移行を招待フィールドを追加し、

def change 
    add_column :users, :invitation_token, :string 
    add_column :users, :invitation_created_at, :datetime 
    add_column :users, :invitation_sent_at, :datetime 
    add_column :users, :invitation_accepted_at, :datetime 
    add_column :users, :invitation_limit, :integer 
    add_column :users, :invited_by_id, :integer 
    add_column :users, :invited_by_type, :string 
    add_index :users, :invitation_token, :unique => true 

    # Allow null encrypted_password 
    change_column_null :users, :encrypted_password, :string, true 
    # Allow null password_salt (add it if you are using Devise's encryptable module) 
    change_column_null :users, :password_salt, :string, true 
end 

6それにこれを追加するための移行を作成します。

7招待状の経路を追加する

# you might have other controllers or put them in a different directory, but it will be similar to this 
devise_for :users, :controllers => { :invitations => 'users/invitations' } 

8招待コントローラ

class Users::InvitationsController < Devise::InvitationsController 
    protected 

    def invite_params 
    params.permit(user: [:email, :first_name, :last_name, :phone_number, :invitation_token, :provider, :skip_invitation]) 
    end 

    def accept_invitation_params 
    params.permit(:password, :password_confirmation, :invitation_token, :first_name, :last_name, :phone_number) 
    end 

end 

9変更うーん、でもそれは存在していた知らなかった

<%= form_for @user, url: user_invitation_path(@user) do |f| %> 
    <%= f.label :first_name, "First Name", class: "control-label col-md-2 col-sm-2 col-xs-12" %> 
    <%= f.text_field :first_name, class: "form-control", :required => true, required: "" %> 

    <%= f.label :last_name, "Last Name", class: "control-label col-md-2 col-sm-2 col-xs-12" %> 
    <%= f.text_field :last_name, class: "form-control", :required => true, required: "" %> 

    <%= f.label :email, "Email", class: "control-label col-md-2 col-sm-2 col-xs-12" %> 
    <%= f.email_field :email, class: "form-control", :required => true, required: "" %> 

    <%= f.label :phone_number, "Phone Number", class: "control-label col-md-2 col-sm-2 col-xs-12" %> 
    <%= f.text_field :phone_number, class: "form-control", :required => true, required: "" %> 

    <%= f.label :password, "Password", class: "control-label col-md-2 col-sm-2 col-xs-12" %> 
    <%= f.password_field :password, class: "form-control", :required => true, required: "" %> 

    <%= f.label :password_confirmation, "Password Confirmation", class: "control-label col-md-2 col-sm-2 col-xs-12" %> 
    <%= f.password_field :password_confirmation, class: "form-control", :required => true, required: "" %> 

    <%= link_to "Cancel", "", class: "btn btn-danger" %> 
    <%= f.submit "Submit", class: "btn btn-success" %> 
<% end %> 
+0

を招待するためのフォームを追加します。私はそれを試してみましょう! – Godzilla74

+0

このように見えます。しかし、いくつかの重いカスタマイズに取り組んでいます。 – Godzilla74

関連する問題