Ruby on Rails 验证输入表单生成错误
Ruby on Rails validation on input form generating errors
我的 Tracks 模型有一个名为 'bpm'
的整数列
当我尝试提交表单以创建新曲目并将其添加到数据库时;出现以下错误:
Field can't be blank
尽管填写了字段并且已经执行了围绕它的验证,但还是会发生这种情况,也许 post 请求中存在错误?
当我不验证 bpm 字段时,post-请求正常运行。
观看次数 > 曲目 > _form.html.haml
.columns
.column.is-8.is-centered
= simple_form_for @track, html: { multipart: true } do |f|
= f.error_notification
.columns
.field.column.is-9
.control
= f.input :name , required: true, input_html: { class: "input"}, wrapper: false, label_html: { class:"label" }
.field.column
.control
= f.input :price, required: true, input_html: { class:"input", maxlength: 7 }, wrapper: false, label_html: { class:"label" }
.field.column
.control
= f.input :bpm, required: true, input_html: {class:"input", maxlength: 3}, wrapper: false, label_html: { class:"label" }
.field
.control
= f.input :description, required: true, input_html: { class:"textarea" }, wrapper: false, label_html: { class:"label" }
.columns
.field.column.is-4
.control
%label.label Genre
.control.has-icons-left
%span.select
= f.input_field :genre, collection: Track::GENRE, prompt: "Select type"
%span.icon.is-small.is-left
%i.fa.fa-tag
.field
.control
%label.label Add images
.file
%label.file-label
= f.input :image, as: :file, input_html: { class:"file-input track-image" }, label: false, wrapper: false
%span.file-cta
%span.file-icon
%i.fa.fa-upload
%span.file-label Choose a file…
%output#list
%hr/
.field.is-grouped
.control
= f.button :submit, class: 'button is-warning'
= link_to 'Cancel', tracks_path, class:'button is-light'
模型 > track.rb
class Track < ApplicationRecord
before_destroy :not_referenced_by_any_line_item
belongs_to :user, optional: true
has_many :line_items
mount_uploader :image, ImageUploader
serialize :image, JSON #sqlite
validates :name, :genre, :price, :bpm, presence: true
validates :description, length: { maximum: 1000, too_long: "Up to %{count} characters allowed"}, presence: true
validates :bpm, length: { maximum: 3 }
validates :price, length: { maximum: 5 }
GENRE = %w{ Trap Hip-Hop R&B Funk Electro-R&B }
private
def not_referenced_by_any_line_item
unless line_items.empty?
errors.add(:base, "Line items present")
throw :abort
end
end
end
跟踪架构:
create_table "tracks", force: :cascade do |t|
t.string "name"
t.text "description"
t.decimal "price", precision: 5, scale: 2, default: "0.0"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "image"
t.integer "user_id"
t.string "genre"
t.integer "bpm"
end
您是否将 TracksController
中的参数列入白名单?
def track_params
params.require(:track).permit(:price, :genre, :bpm)
end
这只是一个例子
我的 Tracks 模型有一个名为 'bpm'
的整数列当我尝试提交表单以创建新曲目并将其添加到数据库时;出现以下错误:
Field can't be blank
尽管填写了字段并且已经执行了围绕它的验证,但还是会发生这种情况,也许 post 请求中存在错误?
当我不验证 bpm 字段时,post-请求正常运行。
观看次数 > 曲目 > _form.html.haml
.columns
.column.is-8.is-centered
= simple_form_for @track, html: { multipart: true } do |f|
= f.error_notification
.columns
.field.column.is-9
.control
= f.input :name , required: true, input_html: { class: "input"}, wrapper: false, label_html: { class:"label" }
.field.column
.control
= f.input :price, required: true, input_html: { class:"input", maxlength: 7 }, wrapper: false, label_html: { class:"label" }
.field.column
.control
= f.input :bpm, required: true, input_html: {class:"input", maxlength: 3}, wrapper: false, label_html: { class:"label" }
.field
.control
= f.input :description, required: true, input_html: { class:"textarea" }, wrapper: false, label_html: { class:"label" }
.columns
.field.column.is-4
.control
%label.label Genre
.control.has-icons-left
%span.select
= f.input_field :genre, collection: Track::GENRE, prompt: "Select type"
%span.icon.is-small.is-left
%i.fa.fa-tag
.field
.control
%label.label Add images
.file
%label.file-label
= f.input :image, as: :file, input_html: { class:"file-input track-image" }, label: false, wrapper: false
%span.file-cta
%span.file-icon
%i.fa.fa-upload
%span.file-label Choose a file…
%output#list
%hr/
.field.is-grouped
.control
= f.button :submit, class: 'button is-warning'
= link_to 'Cancel', tracks_path, class:'button is-light'
模型 > track.rb
class Track < ApplicationRecord
before_destroy :not_referenced_by_any_line_item
belongs_to :user, optional: true
has_many :line_items
mount_uploader :image, ImageUploader
serialize :image, JSON #sqlite
validates :name, :genre, :price, :bpm, presence: true
validates :description, length: { maximum: 1000, too_long: "Up to %{count} characters allowed"}, presence: true
validates :bpm, length: { maximum: 3 }
validates :price, length: { maximum: 5 }
GENRE = %w{ Trap Hip-Hop R&B Funk Electro-R&B }
private
def not_referenced_by_any_line_item
unless line_items.empty?
errors.add(:base, "Line items present")
throw :abort
end
end
end
跟踪架构:
create_table "tracks", force: :cascade do |t|
t.string "name"
t.text "description"
t.decimal "price", precision: 5, scale: 2, default: "0.0"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "image"
t.integer "user_id"
t.string "genre"
t.integer "bpm"
end
您是否将 TracksController
中的参数列入白名单?
def track_params
params.require(:track).permit(:price, :genre, :bpm)
end
这只是一个例子