Add track/{new,create} with file upload for the track
This commit is contained in:
@@ -3,6 +3,22 @@ class TracksController < ApplicationController
|
||||
@track = Track.find params[:id]
|
||||
end
|
||||
|
||||
def new
|
||||
@track = Track.new
|
||||
end
|
||||
|
||||
def create
|
||||
@track = Track.new(:name => params[:track][:name])
|
||||
if params[:track][:file]
|
||||
@track.uploaded_file = params[:track][:file]
|
||||
end
|
||||
if @track.save
|
||||
redirect_to @track
|
||||
else
|
||||
render :new
|
||||
end
|
||||
end
|
||||
|
||||
def stream
|
||||
end
|
||||
end
|
||||
|
@@ -1,3 +1,17 @@
|
||||
class Track < ActiveRecord::Base
|
||||
validates_presence_of :name
|
||||
|
||||
after_create :save_file
|
||||
|
||||
def uploaded_file=(file)
|
||||
@file = file
|
||||
end
|
||||
|
||||
def save_file
|
||||
if @file
|
||||
File.open("#{Rails.root}/data/tracks/#{id}", 'w') do |f|
|
||||
f.write @file.tempfile.read
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@@ -1,4 +1,5 @@
|
||||
= link_to 'Create playlist', new_playlist_path
|
||||
= link_to 'Add a track', new_track_path
|
||||
%ul
|
||||
- @playlists.each do |p|
|
||||
%li
|
||||
|
6
app/views/tracks/new.html.haml
Normal file
6
app/views/tracks/new.html.haml
Normal file
@@ -0,0 +1,6 @@
|
||||
= form_for @track, :html => { :multipart => true } do |t|
|
||||
= t.label :name
|
||||
= t.text_field :name
|
||||
= t.label :file
|
||||
= t.file_field :file
|
||||
= t.submit 'Upload'
|
Reference in New Issue
Block a user