Sebastian's dev blog 

Audio player in ruby on rails

Check out this youtube video I made for ruby on rails
      
      
    

Setup

I wont go through the setup.  I assume you have a rails 7.0 app started.
I'm using rails 7.0.8

Furthermore, have a scaffold created with entities representing music.
I have a title, position for ordering

Take a look at my migration, schema.rb and music.rb model
lang-ruby
      
          
# migration
class CreateMusicsSql < ActiveRecord::Migration[7.0]
  def change
    create_table :musics do |t|
      t.string :name
      t.integer :album_id
      t.timestamps
      t.integer :position
    end
  end
end

# schema.rb
  create_table "musics", force: :cascade do |t|
    t.string "name"
    t.integer "album_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "position"
  end

#models/music.rb
class Music < ApplicationRecord
# this is what we use for uploading/playing music
  has_one_attached :music_file 
  belongs_to :album, optional: true

  def single?
    return :album_id == nil
  end

end
      
    
updated: 2024-02-04 17:31:45 UTC
back to Web Development more tutorials
admin login