英文:
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:
我必须填写注册表格来测试它,但我收到以下错误消息:
Started POST "/inscriptions" for 127.0.0.1 at 2023-06-18 15:44:51 +0200
Processing by InscriptionsController#create as TURBO_STREAM
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"}
TRANSACTION (0.5ms) BEGIN
↳ app/controllers/inscriptions_controller.rb:27:in `block in create'
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]]
↳ app/controllers/inscriptions_controller.rb:27:in `block in create'
TRANSACTION (0.9ms) ROLLBACK
↳ app/controllers/inscriptions_controller.rb:27:in `block in create'
Completed 500 Internal Server Error in 30ms (ActiveRecord: 13.0ms | Allocations: 5257)
ActiveRecord::NotNullViolation (PG::NotNullViolation: ERREUR: une valeur NULL viole la contrainte NOT NULL de la colonne « club_id » dans la relation « inscriptions »
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).
):
以下图片显示了我用于此应用程序的一些代码
The inscription controller:
注册控制器:
class InscriptionsController < ApplicationController
before_action :set_inscription, only: %i[ show edit update destroy ]
# GET /inscriptions or /inscriptions.json
def index
@inscriptions = Inscription.all
end
# GET /inscriptions/1 or /inscriptions/1.json
def show
end
# GET /inscriptions/new
def new
@inscription = Inscription.new
end
# GET /inscriptions/1/edit
def edit
end
# POST /inscriptions or /inscriptions.json
def create
@inscription = Inscription.new(inscription_params)
respond_to do |format|
if @inscription.save
format.html { redirect_to inscription_url(@inscription), notice: "Inscription was successfully created." }
format.json { render :show, status: :created, location: @inscription }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @inscription.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /inscriptions/1 or /inscriptions/1.json
def update
respond_to do |format|
if @inscription.update(inscription_params)
format.html { redirect_to inscription_url(@inscription), notice: "Inscription was successfully updated." }
format.json { render :show, status: :ok, location: @inscription }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @inscription.errors, status: :unprocessable_entity }
end
end
end
# DELETE /inscriptions/1 or /inscriptions/1.json
def destroy
@inscription.destroy
respond_to do |format|
format.html { redirect_to inscriptions_url, notice: "Inscription was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_inscription
@inscription = Inscription.find(params[:id])
end
# Only allow a list of trusted parameters through.
def inscription_params
params.require(:inscription).permit(:club_name, :age, :vorname, :name, :adress, :email, :phone, :gender)
end
end
The model look like this:
模型如下:
class Inscription < ApplicationRecord
belongs_to :club, :optional => true
#validates :club_name, presence: true
validates :name, presence: true
validates :age, presence: true, numericality: { only_integer: true, greater_than: 0 }
validates :email, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP }
validates :phone, presence: true , format: { with: /\A\d{10}\z/ }
end
The club model:
俱乐部模型:
class Club < ApplicationRecord
belongs_to :ClubAdulte
belongs_to :ClubTeevo
belongs_to :ClubJeuneLecteur
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:
Started POST "/inscriptions" for 127.0.0.1 at 2023-06-18 15:44:51 +0200
Processing by InscriptionsController#create as TURBO_STREAM
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"}
TRANSACTION (0.5ms) BEGIN
↳ app/controllers/inscriptions_controller.rb:27:in `block in create'
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]]
↳ app/controllers/inscriptions_controller.rb:27:in `block in create'
TRANSACTION (0.9ms) ROLLBACK
↳ app/controllers/inscriptions_controller.rb:27:in `block in create'
Completed 500 Internal Server Error in 30ms (ActiveRecord: 13.0ms | Allocations: 5257)
ActiveRecord::NotNullViolation (PG::NotNullViolation: ERREUR: une valeur NULL viole la contrainte NOT NULL de la colonne « club_id » dans la relation « inscriptions »
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).
):
The following image shows some of my code I use for this app
The inscription controller:
class InscriptionsController < ApplicationController
before_action :set_inscription, only: %i[ show edit update destroy ]
# GET /inscriptions or /inscriptions.json
def index
@inscriptions = Inscription.all
end
# GET /inscriptions/1 or /inscriptions/1.json
def show
end
# GET /inscriptions/new
def new
@inscription = Inscription.new
end
# GET /inscriptions/1/edit
def edit
end
# POST /inscriptions or /inscriptions.json
def create
@inscription = Inscription.new(inscription_params)
respond_to do |format|
if @inscription.save
format.html { redirect_to inscription_url(@inscription), notice: "Inscription was successfully created." }
format.json { render :show, status: :created, location: @inscription }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @inscription.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /inscriptions/1 or /inscriptions/1.json
def update
respond_to do |format|
if @inscription.update(inscription_params)
format.html { redirect_to inscription_url(@inscription), notice: "Inscription was successfully updated." }
format.json { render :show, status: :ok, location: @inscription }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @inscription.errors, status: :unprocessable_entity }
end
end
end
# DELETE /inscriptions/1 or /inscriptions/1.json
def destroy
@inscription.destroy
respond_to do |format|
format.html { redirect_to inscriptions_url, notice: "Inscription was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_inscription
@inscription = Inscription.find(params[:id])
end
# Only allow a list of trusted parameters through.
def inscription_params
params.require(:inscription).permit(:club_name, :age, :vorname, :name, :adress, :email, :phone, :gender)
end
end
The model look like this:
class Inscription < ApplicationRecord
belongs_to :club, :optional => true
#validates :club_name, presence: true
validates :name, presence: true
validates :age, presence: true, numericality: { only_integer: true, greater_than: 0 }
validates :email, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP }
validates :phone, presence: true , format: { with: /\A\d{10}\z/ }
end
The club model:
class Club < ApplicationRecord
belongs_to :ClubAdulte
belongs_to :ClubTeevo
belongs_to :ClubJeuneLecteur
end
答案1
得分: 0
这意味着您有一个“NOT NULL”约束。
由于您与“optional: true”选项建立了“belongs_to”关联,因此您需要通过添加并执行新的迁移来修复数据库层。
def change
change_column_null :inscriptions, :club_id, null: true
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
def change
change_column_null :inscriptions, :club_id, null: true
end
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论