Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
songs_controller.rb
class SongsController < ApplicationController
before_filter :authenticate_user!
# GET /songs
# GET /songs.json
def index
@songs = Song.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @songs }
end
end
# GET /songs/1
# GET /songs/1.json
def show
@lastsong = Song.last
@firstsong = Song.first
@song = Song.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @song }
end
end
# GET /songs/new
# GET /songs/new.json
def new
@song = Song.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @song }
end
end
# GET /songs/1/edit
def edit
@song = Song.find(params[:id])
end
# POST /songs
# POST /songs.json
def create
@song = current_user.songs.build(params[:song])
respond_to do |format|
if @song.save
format.html { redirect_to @song, notice: 'Song was successfully created.' }
format.json { render json: @song, status: :created, location: @song }
else
format.html { render action: "new" }
format.json { render json: @song.errors, status: :unprocessable_entity }
end
end
end
# PUT /songs/1
# PUT /songs/1.json
def update
@song = Song.find(params[:id])
respond_to do |format|
if @song.update_attributes(params[:song])
format.html { redirect_to @song, notice: 'Song was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @song.errors, status: :unprocessable_entity }
end
end
end
# DELETE /songs/1
# DELETE /songs/1.json
def destroy
@song = Song.find(params[:id])
@song.destroy
respond_to do |format|
format.html { redirect_to songs_url }
format.json { head :no_content }
end
end
def vote_up
@song = Song.find(params[:id])
voter = current_user
respond_to do |format|
if voter.vote_exclusively_for(@song)
format.html { redirect_to @song, notice: 'Liked!' }
format.json { head :ok }
else
format.html { redirect_to @song, notice: 'Something went wrong...' }
format.json { render json: @song.errors, status: :unprocessable_entity }
end
end
end
def vote_down
@song = Song.find(params[:id])
voter = current_user
respond_to do |format|
if voter.vote_exclusively_against(@song)
format.html { redirect_to @song, notice: 'Unliked!'}
format.json { head :ok }
else
format.html { redirect_to @song, notice: 'Something went wrong...' }
format.json { render json: @song.errors, status: :unprocessable_entity }
end
end
end
end
------------
song.rb
class Song < ActiveRecord::Base
belongs_to :user
acts_as_voteable
has_attached_file :music, :storage => :s3, :s3_credentials => "#{Rails.root}/config/s3.yml"
validates_attachment_content_type :music, :content_type => ['application/mp3', 'application/x-mp3', 'audio/mpeg', 'audio/mp3' ],
:message => 'file must be of filetype .mp3!'
validates_attachment_size :music, :less_than => 10.megabytes
def previous_song
self.class.first(:conditions => ["created_at < ?", created_at], :order => "created_at desc")
end
def next_song
self.class.first(:conditions => ["created_at > ?", created_at], :order => "created_at asc")
end
end
------------
user.rb
class User < ActiveRecord::Base
has_many :songs
acts_as_voter
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
end
------------------
index.html.erb (in views/songs)
<h1>Songs</h1>
<table class="table table-striped">
<thead>
<tr>
<th>Name</th>
<th>Submitted at</th>
<th>Contact</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<% @songs.each do |song| %>
<tr>
<td><%= link_to song.name, user_songs_path(song) %></td>
<td><%= song.created_at.strftime("%a, %d %B %Y at %l:%M %p") %></td>
<td><%= song.contact %>
<td>
<%= link_to 'Delete this', user_songs_path(song), :method => :delete, :confirm => 'Are you sure?', :class => 'btn btn-mini btn-danger' %>
</td>
</tr>
<% end %>
</tbody>
</table>
------------------
routes.rb
Artistsurfer::Application.routes.draw do
devise_for :users
resources :users do
resource :songs do
member do
post :vote_up
post :vote_down
end
end
end
resources :songs
root :to => "songs#index"
-----------------
schema.rb
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20120402152146) do
create_table "songs", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "music_file_name"
t.string "music_content_type"
t.integer "music_file_size"
t.datetime "music_updated_at"
t.text "description"
t.string "contact"
t.integer "user_id"
end
create_table "users", :force => true do |t|
t.string "email"
t.string "encrypted_password", :default => "", :null => false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", :default => 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "name"
t.integer "age"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
create_table "votes", :force => true do |t|
t.boolean "vote", :default => false
t.integer "voteable_id", :null => false
t.string "voteable_type", :null => false
t.integer "voter_id"
t.string "voter_type"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "votes", ["voteable_id", "voteable_type"], :name => "index_votes_on_voteable_id_and_voteable_type"
add_index "votes", ["voter_id", "voter_type", "voteable_id", "voteable_type"], :name => "fk_one_vote_per_user_per_entity", :unique => true
add_index "votes", ["voter_id", "voter_type"], :name => "index_votes_on_voter_id_and_voter_type"
end
------------------
rake routes
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session GET /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
vote_up_user_songs POST /users/:user_id/songs/vote_up(.:format) songs#vote_up
vote_down_user_songs POST /users/:user_id/songs/vote_down(.:format) songs#vote_down
user_songs POST /users/:user_id/songs(.:format) songs#create
new_user_songs GET /users/:user_id/songs/new(.:format) songs#new
edit_user_songs GET /users/:user_id/songs/edit(.:format) songs#edit
GET /users/:user_id/songs(.:format) songs#show
PUT /users/:user_id/songs(.:format) songs#update
DELETE /users/:user_id/songs(.:format) songs#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
songs GET /songs(.:format) songs#index
POST /songs(.:format) songs#create
new_song GET /songs/new(.:format) songs#new
edit_song GET /songs/:id/edit(.:format) songs#edit
song GET /songs/:id(.:format) songs#show
PUT /songs/:id(.:format) songs#update
DELETE /songs/:id(.:format) songs#destroy
root / songs#index
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.