使用手机号码而不是使用设备

时间:2015-07-14 09:05:01

标签: ruby-on-rails ruby email authentication devise

我是ruby的新手,我正试着通过观看一些教程来教我自己的红宝石。

上周我开始了解我尝试过的设备,但有没有办法将默认的电子邮件属性更改为手机号码。

我希望有可能我遇到很多事情,但没有什么真正有用的。

我希望能得到帮助。提前谢谢。

new.html.erb注册

<div class="row vertical-offset-100">
      <div class="col-md-4 col-md-offset-4">
<div class="row">
  <div class="span4">
  <%= simple_form_for(resource, :as => resource_name, :url => session_path(resource_name), html: {class: "well"}) do |f| %>

    <fieldset>
      <h1 class="text-center login-title">Get In It's Free!</h1>  
      <%= f.input :first_name, placeholder: "What's Your Awesome Name?" %>
      <%= f.input :last_name, placeholder: 'And Your Beutiful Last Name' %>
      <%= f.input :phone_number, placeholder: 'number' %> 
      <%= f.input :password, placeholder: 'Make A Strengthfull Password' %>
      <%= f.input :password_confirmation, placeholder: 'Make A Strengthfull Password' %>
          <div><%= f.button :submit, "Dale! (Go Ahead)", class: "btn btn-lg btn-info btn-block" %></div>
      <% end %>
      <%= render "devise/shared/links" %>
          <script type="text/javascript" src="/assets/jquery.backstretch.min.js"></script>
    <script>
        $.backstretch("/assets/shifting.jpg", {speed: 500});
    </script>

new.html.erb的会话

<div class="row vertical-offset-100">
      <div class="col-md-4 col-md-offset-4">
<div class="row">
  <div class="span4">
  <%= simple_form_for(resource, :as => resource_name, :url => session_path(resource_name), html: {class: "well"}) do |f| %>

    <fieldset>
      <h1 class="text-center login-title">Get In With Your</h1>  
    <%= f.input :phone_number, placeholder: 'Phone Number' %>
    <%= f.input :password, placeholder: 'Password' %>


    <% if devise_mapping.rememberable? -%>
      <div><%= f.input :remember_me, as: :boolean %></div>
    <% end -%>

    <div><%= f.button :submit, "Just Sign In!", class: "btn btn-lg btn-info btn-block" %></div>
      <% end %>
      <p> 
</p>
              <div class="text-center">
          <%- if devise_mapping.recoverable? && controller_name != 'passwords' && controller_name != 'registrations' %>
  <%= link_to "Forgot your password?", new_password_path(resource_name) %><br />
<% end -%>
<p> 
</p>
<%- if devise_mapping.registerable? && controller_name != 'registrations' %>
  <%= link_to "Sign up Now", new_registration_path(resource_name) %><br />
<% end -%>
      </fieldset>
  </div>
</div>
</div>
</div>
    <script type="text/javascript" src="/assets/jquery.backstretch.min.js"></script>
    <script>
        $.backstretch("/assets/bg.jpg", {speed: 500});
    </script>

devise.rb

Devise.setup do |config|
  config.secret_key = 'e8406bef31a2c5607f3ab4eb0d165f5745a03fca20a9fa7baedd70bc3ffdec638376dda3ac58efb35dbcbaa955b897dcd5519d7458527955af6e9e8d6c9fafb6'

  config.mailer_sender = 'please-change-me-at-config-initializers-devise@example.com'

  require 'devise/orm/active_record'

  config.authentication_keys = [:phone_number]

user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
         has_many :shipments
         has_many :comments

         def full_name
          first_name + " " + last_name
        end
         def email_required?
          false
        end
     end

registration_controller

class RegistrationsController < Devise::RegistrationsController
 private

  def sign_up_params
    params.require(:user).permit(:first_name, :last_name, :phone_number, :password, :password_confirmation)
  end

  def account_update_params
    params.require(:user).permit(:first_name, :last_name, :phone_number, :password, :password_confirmation, :current_password)
  end
end

1 个答案:

答案 0 :(得分:1)

这就是你如何做到的。您需要在config/initializers/devise.rb中添加以下代码:

Devise.setup do |config|
  config.secret_key = "Whatever is your secret key"
  config.authentication_keys = [ :phone_number ]
end

login次观看中,您必须添加以下text_field,而不是像这样的电子邮件字段 -

<%= f.text_field :phone_number, :class => 'Name of your class', :placeholder => "Phone Number" %>  

完成此步骤后,请按照How to disable email validation in rails device进行操作。

将此添加到您的 registration_controller ,而不是您所写的内容:

before_filter :configure_permitted_parameters

protected

def configure_permitted_parameters
  devise_parameter_sanitizer.for(:sign_up) do |u|
    u.permit(:first_name, :last_name, :phone_number, :password, :password_confirmation)
  end

  devise_parameter_sanitizer.for(:account_update) do |u|
    u.permit(:first_name, :last_name, :phone_number, :password, :password_confirmation)
  end
end

那是