繁体   English   中英

Rails:登录用户可以创建新用户

[英]Rails: Logged in user can create a new user

对于我的网站,我试图找到一种方式,如果用户登录,那么他们可以为某人创建用户。 对于该网站,我不希望任何人注册。 出于安全原因,只有用户才能签名。 我无法弄清楚如何做到这一点。 我正在使用设计登录/注册。

我会发布一些我的代码,但我不确定应该发布哪些代码,仍然是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 -%>

这是我猜你如何注册的地方。 我希望这个页面只有在用户登录时才能查看。问题我看到我THNIK用户只有在他们注销时才能进入注册页面。 我希望只有用户登录时才能查看注册页面。不确定这是如何工作的。

<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

就像我说的那样,我不确定我应该发布哪些代码作为指导。 让我知道我是否应该发布更多信息来计算如何做到这一点。

还要注意,我目前在我的用户控制器中没有代码,也许我必须添加东西才能使其工作?

与检查用户是否已登录并打印按钮的方式相同,您可以添加一个新的link_to帮助程序,该辅助程序指向为使其工作而创建的新路径:

# 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>  

您需要创建一个继承自Devise RegistrationsController的新控制器,它将覆盖Devise new方法,并将使用skip_before_action (Rails 5,对于Rails 4使用skip_before_filter )回调,以使其与覆盖方法一起使用:

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

  def new
    super
  end
end

然后在您的路由中添加registrations_controller以使其与Devise一起使用:

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

并且作为记录用户定义GET路由,该路由与第一步中创建的link_to帮助程序中使用的路径匹配:

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

这里有一个回购,看看它是如何工作的。 希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM