ActiveRecord::NotNullViolation 在 InscriptionsController#create 中发生。

huangapple go评论97阅读模式
英文:

ActiveRecord::NotNullViolation in InscriptionsController#create

问题

I just want to check my registrations form in rails 7 in Ubuntu 22, but it is not working as it should, and I'm still getting some error. The app should send with the POST request data to the server after filling the registrations form in.

我只想检查我的Rails 7中的注册表单,在Ubuntu 22中,但它没有按预期工作,我仍然收到一些错误。在填写注册表单后,应用程序应该使用POST请求将数据发送到服务器。

I have to fill the registrations form to test it, but I get the following error message:

我必须填写注册表格来测试它,但我收到以下错误消息:

  1. Started POST "/inscriptions" for 127.0.0.1 at 2023-06-18 15:44:51 +0200
  2. Processing by InscriptionsController#create as TURBO_STREAM
  3. Parameters: {"authenticity_token"=>"[FILTERED]", "inscription"=>{"vorname"=>"Jade", "name"=>"Doe", "age"=>"13", "adress"=>"3 Marguerite Youcenar", "email"=>"jade_doe@gmail.com", "phone"=>"0768324654", "gender"=>"feminin"}, "commit"=>"Envoyer"}
  4. TRANSACTION (0.5ms) BEGIN
  5. app/controllers/inscriptions_controller.rb:27:in `block in create'
  6. Inscription Create (11.6ms) INSERT INTO "inscriptions" ("vorname", "name", "age", "adress", "email", "phone", "gender", "created_at", "updated_at", "club_id") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING "id" [["vorname", "Jade"], ["name", "Doe"], ["age", "13"], ["adress", "3 Marguerite Youcenar"], ["email", "jade_doe@gmail.com"], ["phone", "0768324654"], ["gender", "feminin"], ["created_at", "2023-06-18 13:44:51.944100"], ["updated_at", "2023-06-18 13:44:51.944100"], ["club_id", nil]]
  7. ↳ app/controllers/inscriptions_controller.rb:27:in `block in create'
  8. TRANSACTION (0.9ms) ROLLBACK
  9. ↳ app/controllers/inscriptions_controller.rb:27:in `block in create'
  10. Completed 500 Internal Server Error in 30ms (ActiveRecord: 13.0ms | Allocations: 5257)
  11. ActiveRecord::NotNullViolation (PG::NotNullViolation: ERREUR: une valeur NULL viole la contrainte NOT NULL de la colonne « club_id » dans la relation « inscriptions »
  12. DETAIL: La ligne en échec contient (15, Jade, Doe, 13, 3 Marguerite Youcenar, jade_doe@gmail.com, 0768324654, feminin, 2023-06-18 13:44:51.9441, 2023-06-18 13:44:51.9441, null).
  13. ):

以下图片显示了我用于此应用程序的一些代码
The inscription controller:

注册控制器:

  1. class InscriptionsController < ApplicationController
  2. before_action :set_inscription, only: %i[ show edit update destroy ]
  3. # GET /inscriptions or /inscriptions.json
  4. def index
  5. @inscriptions = Inscription.all
  6. end
  7. # GET /inscriptions/1 or /inscriptions/1.json
  8. def show
  9. end
  10. # GET /inscriptions/new
  11. def new
  12. @inscription = Inscription.new
  13. end
  14. # GET /inscriptions/1/edit
  15. def edit
  16. end
  17. # POST /inscriptions or /inscriptions.json
  18. def create
  19. @inscription = Inscription.new(inscription_params)
  20. respond_to do |format|
  21. if @inscription.save
  22. format.html { redirect_to inscription_url(@inscription), notice: "Inscription was successfully created." }
  23. format.json { render :show, status: :created, location: @inscription }
  24. else
  25. format.html { render :new, status: :unprocessable_entity }
  26. format.json { render json: @inscription.errors, status: :unprocessable_entity }
  27. end
  28. end
  29. end
  30. # PATCH/PUT /inscriptions/1 or /inscriptions/1.json
  31. def update
  32. respond_to do |format|
  33. if @inscription.update(inscription_params)
  34. format.html { redirect_to inscription_url(@inscription), notice: "Inscription was successfully updated." }
  35. format.json { render :show, status: :ok, location: @inscription }
  36. else
  37. format.html { render :edit, status: :unprocessable_entity }
  38. format.json { render json: @inscription.errors, status: :unprocessable_entity }
  39. end
  40. end
  41. end
  42. # DELETE /inscriptions/1 or /inscriptions/1.json
  43. def destroy
  44. @inscription.destroy
  45. respond_to do |format|
  46. format.html { redirect_to inscriptions_url, notice: "Inscription was successfully destroyed." }
  47. format.json { head :no_content }
  48. end
  49. end
  50. private
  51. # Use callbacks to share common setup or constraints between actions.
  52. def set_inscription
  53. @inscription = Inscription.find(params[:id])
  54. end
  55. # Only allow a list of trusted parameters through.
  56. def inscription_params
  57. params.require(:inscription).permit(:club_name, :age, :vorname, :name, :adress, :email, :phone, :gender)
  58. end
  59. end

The model look like this:

模型如下:

  1. class Inscription < ApplicationRecord
  2. belongs_to :club, :optional => true
  3. #validates :club_name, presence: true
  4. validates :name, presence: true
  5. validates :age, presence: true, numericality: { only_integer: true, greater_than: 0 }
  6. validates :email, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP }
  7. validates :phone, presence: true , format: { with: /\A\d{10}\z/ }
  8. end

The club model:

俱乐部模型:

  1. class Club < ApplicationRecord
  2. belongs_to :ClubAdulte
  3. belongs_to :ClubTeevo
  4. belongs_to :ClubJeuneLecteur
  5. end
英文:

I just want to check my registrations form in rails 7 in Ubuntu 22, but it is not working as it should, and I'm still getting some error. The app should send with the POST request data to the server after filling the registrations form in.

I have to fill the registrations form to test it, but I get the following error message:

  1. Started POST "/inscriptions" for 127.0.0.1 at 2023-06-18 15:44:51 +0200
  2. Processing by InscriptionsController#create as TURBO_STREAM
  3. Parameters: {"authenticity_token"=>"[FILTERED]", "inscription"=>{"vorname"=>"Jade", "name"=>"Doe", "age"=>"13", "adress"=>"3 Marguerite Youcenar", "email"=>"jade_doe@gmail.com", "phone"=>"0768324654", "gender"=>"feminin"}, "commit"=>"Envoyer"}
  4. TRANSACTION (0.5ms) BEGIN
  5. app/controllers/inscriptions_controller.rb:27:in `block in create'
  6. Inscription Create (11.6ms) INSERT INTO "inscriptions" ("vorname", "name", "age", "adress", "email", "phone", "gender", "created_at", "updated_at", "club_id") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING "id" [["vorname", "Jade"], ["name", "Doe"], ["age", "13"], ["adress", "3 Marguerite Youcenar"], ["email", "jade_doe@gmail.com"], ["phone", "0768324654"], ["gender", "feminin"], ["created_at", "2023-06-18 13:44:51.944100"], ["updated_at", "2023-06-18 13:44:51.944100"], ["club_id", nil]]
  7. ↳ app/controllers/inscriptions_controller.rb:27:in `block in create'
  8. TRANSACTION (0.9ms) ROLLBACK
  9. app/controllers/inscriptions_controller.rb:27:in `block in create'
  10. Completed 500 Internal Server Error in 30ms (ActiveRecord: 13.0ms | Allocations: 5257)
  11. ActiveRecord::NotNullViolation (PG::NotNullViolation: ERREUR: une valeur NULL viole la contrainte NOT NULL de la colonne « club_id » dans la relation « inscriptions »
  12. DETAIL: La ligne en échec contient (15, Jade, Doe, 13, 3 Marguerite Youcenar, jade_doe@gmail.com, 0768324654, feminin, 2023-06-18 13:44:51.9441, 2023-06-18 13:44:51.9441, null).
  13. ):

The following image shows some of my code I use for this app
The inscription controller:

  1. class InscriptionsController < ApplicationController
  2. before_action :set_inscription, only: %i[ show edit update destroy ]
  3. # GET /inscriptions or /inscriptions.json
  4. def index
  5. @inscriptions = Inscription.all
  6. end
  7. # GET /inscriptions/1 or /inscriptions/1.json
  8. def show
  9. end
  10. # GET /inscriptions/new
  11. def new
  12. @inscription = Inscription.new
  13. end
  14. # GET /inscriptions/1/edit
  15. def edit
  16. end
  17. # POST /inscriptions or /inscriptions.json
  18. def create
  19. @inscription = Inscription.new(inscription_params)
  20. respond_to do |format|
  21. if @inscription.save
  22. format.html { redirect_to inscription_url(@inscription), notice: "Inscription was successfully created." }
  23. format.json { render :show, status: :created, location: @inscription }
  24. else
  25. format.html { render :new, status: :unprocessable_entity }
  26. format.json { render json: @inscription.errors, status: :unprocessable_entity }
  27. end
  28. end
  29. end
  30. # PATCH/PUT /inscriptions/1 or /inscriptions/1.json
  31. def update
  32. respond_to do |format|
  33. if @inscription.update(inscription_params)
  34. format.html { redirect_to inscription_url(@inscription), notice: "Inscription was successfully updated." }
  35. format.json { render :show, status: :ok, location: @inscription }
  36. else
  37. format.html { render :edit, status: :unprocessable_entity }
  38. format.json { render json: @inscription.errors, status: :unprocessable_entity }
  39. end
  40. end
  41. end
  42. # DELETE /inscriptions/1 or /inscriptions/1.json
  43. def destroy
  44. @inscription.destroy
  45. respond_to do |format|
  46. format.html { redirect_to inscriptions_url, notice: "Inscription was successfully destroyed." }
  47. format.json { head :no_content }
  48. end
  49. end
  50. private
  51. # Use callbacks to share common setup or constraints between actions.
  52. def set_inscription
  53. @inscription = Inscription.find(params[:id])
  54. end
  55. # Only allow a list of trusted parameters through.
  56. def inscription_params
  57. params.require(:inscription).permit(:club_name, :age, :vorname, :name, :adress, :email, :phone, :gender)
  58. end
  59. end

The model look like this:

  1. class Inscription < ApplicationRecord
  2. belongs_to :club, :optional => true
  3. #validates :club_name, presence: true
  4. validates :name, presence: true
  5. validates :age, presence: true, numericality: { only_integer: true, greater_than: 0 }
  6. validates :email, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP }
  7. validates :phone, presence: true , format: { with: /\A\d{10}\z/ }
  8. end

The club model:

  1. class Club < ApplicationRecord
  2. belongs_to :ClubAdulte
  3. belongs_to :ClubTeevo
  4. belongs_to :ClubJeuneLecteur
  5. end

答案1

得分: 0

这意味着您有一个“NOT NULL”约束。

由于您与“optional: true”选项建立了“belongs_to”关联,因此您需要通过添加并执行新的迁移来修复数据库层。

  1. def change
  2. change_column_null :inscriptions, :club_id, null: true
  3. end
英文:

It means you have NOT NULL constraint

Since you have belongs_to association with optional: true option, you need fix database layer by adding and executing new migration

  1. def change
  2. change_column_null :inscriptions, :club_id, null: true
  3. end

huangapple
  • 本文由 发表于 2023年6月18日 22:38:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76501079.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定