Ruby Rails 删除数据库并重建后出现错误

Ruby on Rails erros after dropping database and rebuilding

在我决定删除整个数据库以调试一些验证问题之前,我的网站(在我的本地计算机上)运行良好。

现在我有以下错误(除了我原来的错误!):

Nil location provided. Can't build URI.

在观看次数>曲目>index.html.haml

- content_for :header do
  %section.hero.is-warning
    .hero-body
      .container
        %h1.title
          Browse the newest Tracks
.track-index-grid.pt4
  - @tracks.each do |track|
    .track.border-light
      .track-thumb
        =link_to image_tag(track.image_url(:thumb)), track #Error occurs on this line
        - if track.genre
          .condition
            %span.tag.is-dark
              = track.genre
        - if track.bpm
          .condition
            %span.tag.is-dark
              = track.bpm
      .pa3
        %h3.fw7.f4.title= link_to track.name, track
        %p.has-text-gray.fg.pt1
          Sold by #{track.user.name}
        %p.f3.fw6.has-text-right.pt2.price= number_to_currency(track.price)
        - if track_artist(track)
          = link_to 'Edit', edit_track_path(track), class: "button is-small"
          = link_to 'Delete', track, method: :delete, data: { confirm: "Are you sure ?" }, class: "button is-small"

Field can't be blank 尝试填写时提交 bpm 字段:

模型 > track.rb:

class Track < ApplicationRecord
  mount_uploader :image, ImageUploader
  serialize :image, JSON #sqlite
  belongs_to :user, optional: true
  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 }
end

观看次数>曲目> _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'

schema.rb(通过迁移创建):

ActiveRecord::Schema.define(version: 2018_12_07_180556) do

  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

  create_table "users", force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "name"
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

end

这是因为 image_url 返回 nil。您删除了 PaperClip(?) 数据。在调用 image_tag.

之前,您必须检查 nil
-unless track.image_url(:thumb).nil?
  =link_to image_tag(track.image_url(:thumb)), track