2017-06-07 18 views
1

私のウェブサイトでは、ユーザーがログインしている場合にユーザーを作成する方法を見つけようとしています。ウェブサイトについては、誰にもサインアップしないだけです。セキュリティ上の理由から、ユーザーだけが誰かにサインインすることができます。私はそれを行う方法を理解することはできません。私はログイン/サインアップのためにdeviseを使用しています。Rails:ログインしたユーザーは新しいユーザーを作成できます

私のコードの一部を投稿しますが、投稿する必要があるものはまだわかりません。まだ新しいレールです。

ここでは、ユーザー/サインアップを作成するリンクが必要です。サインアップページは、ユーザーがログインしている場合にのみ表示する必要があります。

<!DOCTYPE html> 
<html> 
... 
    <body> 
    <div class = "auth"> 
     <% if !user_signed_in? %> 
     <button class = "signed_in"><%= link_to "Login", new_user_session_path %></button> 
    <% end %> 

    <% if user_signed_in? %> 
     <div class = "buttons"> 
      <button class = "button"><%= link_to("Logout", destroy_user_session_path, :method => :delete) %> </button> 

     </div> 
    <% end %> 
    </div> 
    <p class="notice"><%= notice %></p> 
    <p class="alert"><%= alert %></p> 

    <%= yield %> 
    </body> 
</html> 

これも投稿します。私はこれがリンクが認証にどのように作用するのかと思います。

<%- if controller_name != 'sessions' %> 
    <%= link_to "Log in", new_session_path(resource_name) %><br /> 
<% end -%> 

<%- if devise_mapping.registerable? && controller_name != 'registrations' %> 
    <%= link_to "Sign up", new_registration_path(resource_name) %><br /> 
<% end -%> 

<%- if devise_mapping.recoverable? && controller_name != 'passwords' && controller_name != 'registrations' %> 
    <%= link_to "Forgot your password?", new_password_path(resource_name) %><br /> 
<% end -%> 

<%- if devise_mapping.confirmable? && controller_name != 'confirmations' %> 
    <%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name) %><br /> 
<% end -%> 

<%- if devise_mapping.lockable? && resource_class.unlock_strategy_enabled?(:email) && controller_name != 'unlocks' %> 
    <%= link_to "Didn't receive unlock instructions?", new_unlock_path(resource_name) %><br /> 
<% end -%> 

<%- if devise_mapping.omniauthable? %> 
    <%- resource_class.omniauth_providers.each do |provider| %> 
    <%= link_to "Sign in with #{OmniAuth::Utils.camelize(provider)}", omniauth_authorize_path(resource_name, provider) %><br /> 
    <% end -%> 
<% end -%> 

ここで私があなたのサインアップ方法を推測します。このページは、ユーザーがログインしている場合にのみ表示することができます。ユーザーがログアウトしている場合、ユーザーはサインアップページにアクセスすることができます。ユーザーがログインしている場合にのみサインアップページを表示することができます。どのように動作するのかわかりません。

<article class = "sign" > 

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> 
    <%= devise_error_messages! %> 

    <div class="field"> 
    <%= f.label :email %><br /> 
    <%= f.email_field :email, autofocus: true %> 
    </div> 

    <div class="field"> 
    <%= f.label :password %> 
    <% if @minimum_password_length %> 
    <em>(<%= @minimum_password_length %> characters minimum)</em> 
    <% end %><br /> 
    <%= f.password_field :password, autocomplete: "off" %> 
    </div> 

    <div class="field"> 
    <%= f.label :password_confirmation %><br /> 
    <%= f.password_field :password_confirmation, autocomplete: "off" %> 
    </div> 

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


<%= render "devise/shared/links" %> 
</article> 

私も、私は私が指導のために投稿すべきかのコードを正確にわからない、言ったように、重要な

class DeviseCreateUsers < ActiveRecord::Migration[5.1] 
    def change 
    create_table :users do |t| 
     ## Database authenticatable 
     t.string :email,    null: false, default: "" 
     t.string :encrypted_password, null: false, default: "" 

     ## Recoverable 
     t.string :reset_password_token 
     t.datetime :reset_password_sent_at 

     ## Rememberable 
     t.datetime :remember_created_at 

     ## Trackable 
     t.integer :sign_in_count, default: 0, null: false 
     t.datetime :current_sign_in_at 
     t.datetime :last_sign_in_at 
     t.string :current_sign_in_ip 
     t.string :last_sign_in_ip 

     ## Confirmable 
     # t.string :confirmation_token 
     # t.datetime :confirmed_at 
     # t.datetime :confirmation_sent_at 
     # t.string :unconfirmed_email # Only if using reconfirmable 

     ## Lockable 
     # t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts 
     # t.string :unlock_token # Only if unlock strategy is :email or :both 
     # t.datetime :locked_at 


     t.timestamps null: false 
    end 

    add_index :users, :email,    unique: true 
    add_index :users, :reset_password_token, unique: true 
    # add_index :users, :confirmation_token, unique: true 
    # add_index :users, :unlock_token,   unique: true 
    end 
end 

に見える、移行コードを掲載しました。私がこれを行う方法を理解するためにさらに投稿する必要があるかどうかを教えてください。

また、私のユーザーコントローラには現在コードがありません。この作業をするために追加する必要があるかもしれません。

# html.erb file (mostly application.html.erb) 
<div class="auth"> 
    <% if !user_signed_in? %> 
    <button class="signed_in"> 
     <%= link_to 'Login' new_user_session_path %> 
    </button> 
    <% else %> 
    <div class="buttons"> 
     <button class="button"> 
     <%= link_to('Logout', destroy_user_session_path, method: :delete) %> 
     </button> 
     <button class="button"> 
     <%= link_to 'Create a new account', new_account_path %><br /> 
     </button> 
    </div> 
    <% end %> 
</div> 

あなたに:

+0

これはどういうわけかhttps://stackoverflow.com/questions/23140117/allow-a-user-to-add-new-users-in-devise-and-remain-logged-in-as-themselves – Pavan

答えて

0

あなたは、それはユーザーが署名されているかどうかをチェックし、ボタンを印刷するように行うのと同じように、あなたはこの仕事をするために作成された新しいルートを指す新しいlink_toヘルパーを追加することができますそれがオーバーライドメソッドで動作させるためにコールバック考案newメソッドをオーバーライドし、skip_before_action(レール4使用skip_before_filter用レール5を)使用する考案RegistrationsControllerから継承する新しいコントローラを作成する必要があります。

# app/controllers/registrations_controller.rb 
class RegistrationsController < Devise::RegistrationsController 
    skip_before_action :require_no_authentication, only: [:new] 

    def new 
    super 
    end 
end 

は、その後、あなたのルートで、あなたはそれを考案して動作させるためにregistrations_controllerを追加します。

# config/routes.rb 
devise_for :users, controllers: { registrations: 'registrations' } 

そして、ログインしたユーザーの最初のステップで作成したlink_toヘルパーで使用すると一致GETルート定義として:

# config/routes.rb 
as :user do 
    get '/new_account', to: 'registrations#new' 
end 

Hereどのように動作するか見るためのレポがあります。それが役に立てば幸い。

+0

登録のための新しいコントローラを作成するコマンドは何でしょうか?コントローラーを生成するレールになりますか? – bockdavidson

+0

コマンドを実行するには、 'rails generate controller Registrations'コマンドで新しいものを作成し、関連するすべてのファイルを作成するか、手作業で行うことができます。 –

+0

ありがとう!出来た!また、特定のユーザーだけが自分の電子メールアドレスに基づいて新しいユーザーを作成できる方法はありますか? – bockdavidson

関連する問題