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

时间:2014-11-25 03:50:02

标签: ruby-on-rails activerecord

我有一个表格,允许用户(代理商)注册属于该用户(代理商)的其他用户(客户)。

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

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

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

index.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>

有关clients.create的深入了解,这里是用户模型和关系模型

User.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

1 个答案:

答案 0 :(得分:0)

与参数show的转储类似,您所追求的值的哈希嵌套在“add_clients”中:

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

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

但是,我还会检查Strong Parameters上的文档以及如何在Rails中组织RESTful routing/controllers,因为您使用代码破坏了许多已建立的模式。