Ruby on rails 验证失败:电子邮件可以';t为空,密码可以';不要为空-不要为空

Ruby on rails 验证失败:电子邮件可以';t为空,密码可以';不要为空-不要为空,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,我有一个表单,允许用户(代理)注册属于该用户(代理)的另一个用户(客户端) 为了创建该用户(客户端),我创建了一个表单,该表单通过params将所有凭据传递给控制器操作 但是,当我尝试使用这些凭据创建新用户时,出现以下错误: ActiveRecord::RecordInvalid in AddClientsController#new Validation failed: Email can't be blank, Password can't be blank app/controller

我有一个表单,允许用户(代理)注册属于该用户(代理)的另一个用户(客户端)

为了创建该用户(客户端),我创建了一个表单,该表单通过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"}
添加客户端控制器.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
class AddClientsControllerparams[:email],:name=>params[:name],:password=>params[:password],:password\u confirmation=>params[:password\u confirmation]})
重定向到仪表板路径
结束
def安全参数
参数许可证(:电子邮件,:姓名,:密码,:角色)
结束
结束
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>

{:action=>'new'})do | f |%>
添加新客户端
true,类:'窗体控件'%>
true,类:'窗体控件'%>
'右键'%>
为了深入了解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
class用户:新建记录?
#包括默认设计模块。其他可供选择的项目包括:
#:可确认、:可锁定、:超时和:全授权
设计:数据库可验证,可注册,
:可恢复,:可记忆,:可跟踪,:可验证
序列化:adwords\u令牌
有很多:客户关系,外键:“客户id”
拥有多个:客户,通过::客户关系
有很多:代理关系,类名:“客户关系”,外键:“代理id”
有很多:代理,通过::代理关系
#添加的参数角色为self.role
def set_默认_角色
self.role | |=:代理|主
结束
结束
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
类ClientRelationship与参数转储一样,您要查找的值的散列嵌套在“添加客户端”中:

因此,要获得电子邮件的价值,代码应该是
params[:add_clients][:email]
等等


但是,当您的代码破坏了许多已建立的模式时,我还将检查有关Rails的文档以及如何在Rails中进行组织。

stacktrace告诉我们您的错误在第11行,但您的控制器代码在该行中没有新操作,因此请添加您的整个控制器代码。不过我没有发现新动作有什么问题。谢谢你的回复。这是整个控制器代码。我不知道为什么它说的是
11
,而不是
6
。我想我可能是从打开的窗口中的早期错误复制了跟踪。现在它是正确的。在哪一行抛出异常?谢谢Pete,我不敢相信我错过了。谢谢你的补充意见,我会努力改进这些方面。
"add_clients"=>{
   "name"=>"test 3",
   "email"=>"testform3@gmail.com",
   ...
}