Add track/{new,create} with file upload for the track

This commit is contained in:
Thibault Jouan
2011-07-23 17:55:25 +00:00
parent 34b38b77cf
commit 6af96b0f75
12 changed files with 159 additions and 0 deletions

View File

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

View File

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

View File

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

View 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'