设计注册表单显示即使在输入名字后,名字也不能为空

时间:2017-07-04 20:20:28

标签: ruby-on-rails forms devise

我已使用first_name和last_name字段创建了用户迁移。我还为User生成了Devise视图。在Devise Sign Up表单中,我编辑了第一个名字和姓氏字段。当我尝试注册时,当我按下注册时,它显示first_name和Last_name不能为空,即使这些字段不是空白。 用户模型:

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

  validates :first_name, :last_name, :email,  presence: true
end

用户迁移:

class CreateUsers < ActiveRecord::Migration[5.1]
  def change
    create_table :users do |t|
      t.string :first_name
      t.string :last_name
      t.string :email 
    end
  end
end

设计注册表单:

[![<h2>Sign up</h2>

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

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

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

  <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" %>][1]][1]

1 个答案:

答案 0 :(得分:1)

您需要将它们添加到设计控制器的强参数中。

您可以创建自己的(扩展Devise),然后:

 class RegistrationsController < Devise::RegistrationsController

 private

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

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

Here's a good guide

祝你好运!