Ошибки Ruby on Rails после удаления базы данных и восстановления - PullRequest
0 голосов
/ 07 декабря 2018

Мой веб-сайт работал нормально (на моем локальном компьютере), прежде чем я решил отбросить всю свою базу данных, чтобы устранить некоторые проблемы с проверкой.

Теперь у меня есть следующие ошибки (в дополнение к моим исходным)!):

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:

models> 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

views> track> _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

1 Ответ

0 голосов
/ 07 декабря 2018

Это потому, что image_url возвращает nil.Вы удалили данные PaperClip (?).Вы должны проверить на nil, прежде чем позвонить image_tag.

-unless track.image_url(:thumb).nil?
  =link_to image_tag(track.image_url(:thumb)), track
...