在Rails中添加没有注册表单的用户

时间:2018-08-07 17:59:42

标签: ruby-on-rails model-view-controller ruby-on-rails-5

我有一个用户注册表单,一旦用户注册,他就会被重定向到用户详细信息表单。

用户注册表格。

<%= form_for(@user, :html => { multipart: true, class: "form-horizontal", role: "form"}) do |f| %>
      <%= f.label :email, class: "required" %>
      <%= f.email_field :email, required: true %>
      <%= f.label :password, class: "required" %>
      <%= f.password_field :password, required: true %>
      <%= f.label :password_confirmation, class: "required" %>
      <%= f.password_field :password_confirmation, required: true %>
      <%= f.submit 'Sign up' %>
<% end %>

用户详细信息表格

<%= form_for(@user, :html => { multipart: true, class: "form-horizontal", role: "form"}) do |f| %>
   <%= f.label :user_firstname, "First Name", class: "required"  %>
   <%= f.text_field :user_firstname, required: true %>
   <%= f.label :user_lastname, "Last Name", class: "required"  %>
   <%= f.text_field :user_lastname, required: true %>
   <%= f.submit 'Save' %>            
<% end %>

我想添加一个功能,以便管理员可以添加没有电子邮件的用户,而管理员只需在用户详细信息表单中添加详细信息,然后提交并创建用户。 我所做的是,我在用户控制器中添加了一条路由并添加了一个方法,这是我得到的错误。添加族时出错。重试。请帮我解决这个问题

路线

get 'adduser', to:'users#adduser'
post 'adduser', to:'users#create_user' 

用户控制器

def adduser
  @user=User.new
end
def create_user
  @user=User.new(user_params)
  if @user.save
    flash[:success] = "User successfully added" redirect_to main_admin_path
  else
    flash[:danger] = "There was an error adding user. Try again" redirect_to main_admin_path
  end
end

<%= form_for(@user, url: adduser_path, :html => { multipart: true, class: "form-horizontal", role: "form"}) do |f| %>
       <%= f.label :user_firstname, "First Name", class: "required"  %>
       <%= f.text_field :user_firstname, required: true %>
       <%= f.label :user_lastname, "Last Name", class: "required"  %>
       <%= f.text_field :user_lastname, required: true %>
       <%= f.submit 'Save' %>            
<% end %>

验证

attr_accessor :remember_token, :activation_token, :reset_token
before_create :confirmation_token
has_secure_password
validates :email, presence: true, uniqueness: {case_sensitive: false}, format: { with: VALID_EMAIL_REGEX }
validates_attachment :document
validates_confirmation_of :password

2 个答案:

答案 0 :(得分:0)

首先,您需要创建一个迁移并将新列添加到users表。我认为最好将有关admin创建的信息保留在数据库中(如果您不想存储它,只需将:created_by_admin添加到attr_accessor列表中即可,而不是此步骤)。

add_column :users, :created_by_admin, :boolean, default: false, null: false

将此新字段添加到表单(“由管理员添加”):

<%= f.hidden_field :created_by_admin, value: true %>

为电子邮件验证添加条件:

validates :email, presence: true, uniqueness: { case_sensitive: false },
  format: { with: VALID_EMAIL_REGEX }, unless: :created_by_admin?

答案 1 :(得分:0)

如果您不想对现有架构进行任何更改,请在模型中使用attr_accessor

models / user.rb

class User
  attr_accessor :with_email_optional

  # Run these validations only if `self.with_email_optional` is not set OR set to `false`
  validates :email,
            presence: true,
            uniqueness: { case_sensitive: false },
            format: { with: VALID_EMAIL_REGEX },
            unless: :with_email_optional
end

在您的用户控制器中,用于管理员(我不知道您的应用程序中所谓的名称):

def new
  @user = User.new
  @user.with_email_optional = true
end

private

# To be used in `create` action
def user_params
  params.require(:user).permit(..., :with_email_optional)
end

表格

<%= form_for(@user) do |f|
  <%= f.hidden_field :with_email_optional, value: f.object.with_email_optional %>
<% end %>