验证失败:电子邮件不能为空,密码不能为空 - 不为空



我有一个表格,允许用户(代理机构)注册属于该用户(代理机构)的另一个用户(客户)。

为了创建该用户(客户端),我创建了一个表单,该表单通过参数将所有凭据传递给控制器操作。

但是,当我尝试使用这些凭据创建新用户时,出现以下错误:

ActiveRecord::RecordInvalid in AddClientsController#new
Validation failed: Email can't be blank, Password can't be blank
app/controllers/add_clients_controller.rb:6:in new

但是我可以清楚地看到电子邮件和密码正在传递。

以下是请求中的参数:

参数:

{"utf8"=>"✓",
 "authenticity_token"=>"d1beVLphq5B5P51BxeVmSYS42NIgBb6m1mLtiQPc7SI=",
 "add_clients"=>{"name"=>"test 3",
 "email"=>"testform3@gmail.com",
 "password"=>"[FILTERED]",
 "password_confirmation"=>"[FILTERED]",
 "role"=>"agent_admin"},
 "commit"=>"Submit"}

add_clients_controller.rb

class AddClientsController < ApplicationController
  def index
  end
  def new
    current_user.clients.create!({:email => params[:email], :name => params[:name], :password => params[:password], :password_confirmation => params[:password_confirmation]})
    redirect_to dashboards_path
  end

  def secure_params
    params.permit(:email, :name, :password, :role)
  end
end

索引中的表单.html.erb

<div class="authform">
  <%= form_for(:add_clients, :url => {:action => 'new'}) do |f| %>
    <h3>Add A New Client</h3>
    <div class="form-group">
      <%= f.label :name %>
      <%= f.text_field :name, :autofocus => true, class: 'form-control' %>
    </div>
    <div class="form-group">
      <%= f.label :email %>
      <%= f.email_field :email, :autofocus => true, class: 'form-control' %>
    </div>
    <div class="form-group">
      <%= f.label :password %>
      <%= f.password_field :password, class: 'form-control' %>
    </div>
    <div class="form-group">
      <%= f.label :password_confirmation %>
      <%= f.password_field :password_confirmation, class: 'form-control' %>    
    </div>
    <div class="form-group">
      <%= f.select(:role, User.roles.keys.map {|role| [role.titleize,role]}) %>
    </div>
     <%= f.submit 'Submit', :class => 'button right' %>
  <% end %>
</div>

为了深入了解 client.create,这里是用户模型和关系模型

用户.rb

class User < ActiveRecord::Base
  # Declare an enum attribute where the values map to integers in the database, but can be queried by name.
  enum role: [:application_admin, :agency_master, :agency_admin, :agent_admin, :client_admin, :agent_user, :client_user]
  after_initialize :set_default_role, :if => :new_record?
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  serialize :adwords_token
  has_many :client_relationships,  foreign_key: 'client_id'                                                                                      
  has_many :clients, through: :client_relationships                                                                                              
  has_many :agent_relationships, class_name: 'ClientRelationship', foreign_key: 'agency_id'                                                      
  has_many :agencies, through: :agent_relationships  
  #Added params role was self.role
  def set_default_role
    self.role ||= :agency_master
  end
end

client_relationship.rb

class ClientRelationship < ActiveRecord::Base
  belongs_to :agency, class_name: "User", foreign_key: 'client_id'                                                                               
  belongs_to :client, class_name: "User", foreign_key: 'agency_id' 
end

就像参数的转储显示的那样,你所追求的值的哈希值嵌套在"add_clients"中:

"add_clients"=>{
   "name"=>"test 3",
   "email"=>"testform3@gmail.com",
   ...
}

因此,要获得电子邮件的值,代码将params[:add_clients][:email]等。

但是,我也会查看有关强参数的文档以及如何在 Rails 中组织 RESTful 路由/控制器,因为您正在用代码破坏许多已建立的模式。

最新更新