My Tracks model has an integer column called 'bpm'
When I try to submit the form to create a new track and add it to the database; the following error occurs:
Field can't be blank
This occurs despite the field being filled in and the validations surrounding it have been carried out, perhaps there's an error in the post request?
The post-request functions correctly when I do not validate the bpm field.
views > tracks > _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'
models > 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
track schema:
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
Have you whitelisted the parameter in TracksController
?
def track_params
params.require(:track).permit(:price, :genre, :bpm)
end
This is just an example
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.