输入表单上的Ruby on Rails验证会产生错误

时间:2018-12-08 14:30:09

标签: ruby-on-rails ruby haml

“我的足迹”模型有一个名为“ bpm”的整数列

当我尝试提交表单以创建新的曲目并将其添加到数据库时;发生以下错误:

Field can't be blank

尽管填写了该字段并且已经对其进行了验证,但还是会发生这种情况,也许发帖请求中有错误?

当我不验证bpm字段时,请求后功能可以正常运行。

视图>曲目> _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

1 个答案:

答案 0 :(得分:1)

您是否已将TracksController中的参数列入白名单?

def track_params
  params.require(:track).permit(:price, :genre, :bpm)
end

这只是一个例子