Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在rails中包含带有params.permit的硬编码字段值_Ruby On Rails - Fatal编程技术网

Ruby on rails 如何在rails中包含带有params.permit的硬编码字段值

Ruby on rails 如何在rails中包含带有params.permit的硬编码字段值,ruby-on-rails,Ruby On Rails,我得到以下错误: def create employee = current_admin.organization.admins.new(employee_params , code: "91") employee.save end 那么,如何为字段代码添加硬编码值呢 *** ArgumentError Exception: wrong number of arguments (given 2, expected 0..1) 但最好在模型中有这样的逻辑,比如 emp

我得到以下错误:

def create
 employee = current_admin.organization.admins.new(employee_params , code: "91")
 employee.save 
end
那么,如何为字段代码添加硬编码值呢

*** ArgumentError Exception: wrong number of arguments (given 2, expected 0..1)
但最好在模型中有这样的逻辑,比如

employee = current_admin.organization.admins.new(employee_params.merge(code: "91"))
试一试

但最好在模型中有这样的逻辑,比如

employee = current_admin.organization.admins.new(employee_params.merge(code: "91"))

它可以添加为db级别的默认值

before_create :add_code

def add_code
  self.code = '91'
end

您可以在创建或保存之前使用回调

change_column :employee, :code, :string, :default => "91"
#app/models/employee.rb
类员工<应用程序记录
保存前:设置默认值
def set_默认值
self.code | |='91'
结束
结束

它可以作为默认值添加到db级别

before_create :add_code

def add_code
  self.code = '91'
end

您可以在创建或保存之前使用回调

change_column :employee, :code, :string, :default => "91"
#app/models/employee.rb
类员工<应用程序记录
保存前:设置默认值
def set_默认值
self.code | |='91'
结束
结束