Improve tracks CRUD
* Add index, edit and update action; * Add _form partial; * Add link to tracks/index in nav; * Refactor specs.
This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
class TracksController < ApplicationController
|
||||
def index
|
||||
@tracks = Track.all
|
||||
end
|
||||
|
||||
def show
|
||||
@track = Track.find(params[:id])
|
||||
end
|
||||
@@ -7,6 +11,10 @@ class TracksController < ApplicationController
|
||||
@track = Track.new
|
||||
end
|
||||
|
||||
def edit
|
||||
@track = Track.find(params[:id])
|
||||
end
|
||||
|
||||
def create
|
||||
@track = Track.new(track_params)
|
||||
|
||||
@@ -17,6 +25,17 @@ class TracksController < ApplicationController
|
||||
end
|
||||
end
|
||||
|
||||
def update
|
||||
@track = Track.find(params[:id])
|
||||
|
||||
if @track.update_attributes track_params
|
||||
redirect_to action: :index
|
||||
else
|
||||
render :edit
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
def track_params
|
||||
|
@@ -12,6 +12,8 @@
|
||||
%ul
|
||||
%li
|
||||
= link_to 'Playlists', playlists_path
|
||||
%li
|
||||
= link_to 'Tracks', tracks_path
|
||||
%li
|
||||
= link_to 'Sign out', signout_path
|
||||
|
||||
|
13
app/views/tracks/_form.html.haml
Normal file
13
app/views/tracks/_form.html.haml
Normal file
@@ -0,0 +1,13 @@
|
||||
= form_for @track, html: { multipart: true } do |t|
|
||||
%table
|
||||
%tbody
|
||||
%tr
|
||||
%th= t.label :name
|
||||
%td= t.text_field :name
|
||||
- if @track.new_record?
|
||||
%tr
|
||||
%th= t.label :file
|
||||
%td= t.file_field :file
|
||||
%tfoot
|
||||
%tr
|
||||
%td= t.submit submit_text
|
1
app/views/tracks/edit.html.haml
Normal file
1
app/views/tracks/edit.html.haml
Normal file
@@ -0,0 +1 @@
|
||||
= render 'form', submit_text: 'Save'
|
6
app/views/tracks/index.html.haml
Normal file
6
app/views/tracks/index.html.haml
Normal file
@@ -0,0 +1,6 @@
|
||||
= link_to 'Create track', new_track_path
|
||||
|
||||
%ul
|
||||
- @tracks.each do |p|
|
||||
%li
|
||||
= link_to p.name, track_path(p)
|
@@ -1,12 +1 @@
|
||||
= form_for @track, html: { multipart: true } do |t|
|
||||
%table
|
||||
%tbody
|
||||
%tr
|
||||
%th= t.label :name
|
||||
%td= t.text_field :name
|
||||
%tr
|
||||
%th= t.label :file
|
||||
%td= t.file_field :file
|
||||
%tfoot
|
||||
%tr
|
||||
%td= t.submit 'Upload'
|
||||
= render 'form', submit_text: 'Upload'
|
||||
|
@@ -1,5 +1,9 @@
|
||||
%h1= @track.name
|
||||
|
||||
- if @track.sound?
|
||||
%audio{src: sound_path(@track.sound), controls: true, autoplay: true}
|
||||
Your browser does not support the audio element
|
||||
%p
|
||||
%audio{src: sound_path(@track.sound), controls: true, autoplay: true}
|
||||
Your browser does not support the audio element
|
||||
|
||||
%p
|
||||
= link_to 'Edit', edit_track_path(@track)
|
||||
|
Reference in New Issue
Block a user