ruby-on-rails - 在控制器中创建操作省略了参考字段
问题描述
当我为环境创建客户时,我的 Rails 应用程序出现问题,如果省略environment_id
插入语句中的参数,则操作本身存在问题。
这是我的customer.rb
模型:
class Customer < ActiveRecord::Base
belongs_to :environment, inverse_of: :customers
has_many :all_services, class_name: 'Service'
has_many :services, inverse_of: :customer
has_paper_trail ignore: %i[created_at updated_at]
end
这是我的environment.rb
模型:
class Environment < ActiveRecord::Base
has_many :all_customers, class_name: 'Customer', dependent: :destroy
has_many :customers, inverse_of: :environment
has_many :all_services, class_name: 'Service', dependent: :destroy
has_many :services, inverse_of: :environment
has_many :all_versions, class_name: 'Version', dependent: :destroy
has_many :versions, inverse_of: :environment
has_many :all_role_permissions, class_name: 'RolePermission', dependent: :destroy
has_many :role_permissions, inverse_of: :environment
has_paper_trail ignore: %i[created_at updated_at]
end
这是customer_controller.rb
创建操作:
def create
if customer_params.permitted?
render json: Customer.create!(customer_params), status: :ok
else
render json: { message: Api::V1::INVALID_PARAMETERS }, status: :bad_request
end
end
def customer_params
params.require(:customer).permit(:environment_id, :full_name, :document_type, :document_value, :customer_type)
end
这是服务器日志:
Started POST "/api/v1/customers" for 127.0.0.1 at 2021-06-02 18:18:44 +0100
Processing by Api::V1::CustomersController#create as HTML
Parameters: {"full_name"=>"cvbcv", "document_type"=>"bcvbc", "document_value"=>"vbcvbcv", "customer_type"=>"bcvbcvb", "environment_id"=>1, "customer"=>{"full_name"=>"cvbcv", "document_type"=>"bcvbc", "document_value"=>"vbcvbcv", "customer_type"=>"bcvbcvb", "environment_id"=>1}}
User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."uid" = $1 LIMIT $2 [["uid", "test1@test.cl"], ["LIMIT", 1]]
(0.5ms) BEGIN
↳ app/services/api/customers.rb:20:in `create_customer'
Environment Load (0.5ms) SELECT "environments".* FROM "environments" WHERE "environments"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ app/services/api/customers.rb:20:in `create_customer'
Customer Create (1.4ms) INSERT INTO "customers" ("full_name", "document_type", "document_value", "customer_type", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["full_name", "cvbcv"], ["document_type", "bcvbc"], ["document_value", "vbcvbcv"], ["customer_type", "bcvbcvb"], ["created_at", "2021-06-02 17:18:44.378509"], ["updated_at", "2021-06-02 17:18:44.378509"]]
↳ app/services/api/customers.rb:20:in `create_customer'
(1.2ms) ROLLBACK
↳ app/services/api/customers.rb:20:in `create_customer'
Completed 500 Internal Server Error in 22ms (ActiveRecord: 4.3ms | Allocations: 6384)
ActiveRecord::NotNullViolation (PG::NotNullViolation: ERROR: null value in column "environment_id" violates not-null constraint
DETAIL: Failing row contains (28, cvbcv, bcvbc, vbcvbcv, bcvbcvb, null, 2021-06-02 17:18:44.378509, 2021-06-02 17:18:44.378509).
):
我尝试了我能知道的一切,我三重检查了迁移和架构。我还检查了以前项目的活动记录 gem 版本,一切正常。
可能相关的一件事是,该项目在 Angular 中有一个前端。
解决方案
Rails 验证数据库中的环境存在,因为belongs_to
它是客户。
您日志中的以下查询正在尝试使用以下内容加载环境ID=1
:
Environment Load (0.5ms) SELECT "environments".* FROM "environments" WHERE "environments"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
在此 Rails 尝试在INSERT
没有环境参考的情况下尝试新客户,因为似乎 ID=1 的环境在数据库中不存在。
推荐阅读
- android - 如何在 WKWebView 和 Android WebView 等智能手机的 webview 上使用 VueRouter?
- javascript - 新页面完全加载后如何单击按钮?
- angular - 创建组件并在应用程序中的每个位置使用时通知文本消息不起作用
- javascript - 点击链接内的按钮
- matlab - 按其(数字)列之一对混合文本数字矩阵(元胞数组)进行排序
- nlp - 如何用关键词组成有意义的句子?
- reactjs - 使用 webpack 的语法错误和来自不同目录的反应组件,意外的令牌 (8:12)
- iis - How can I get multiple query string from URL when redirecting in IIS?
- javascript - 如何将新的 li 添加到 ul 并删除旧的
- gremlin - 有没有办法以 hashmap 的形式从 gremlin 返回数据?