Created
July 24, 2012 08:10
-
-
Save freshfey/3168757 to your computer and use it in GitHub Desktop.
Songs Controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: user_song_url } | |
end | |
end | |
# GET /songs/new | |
# GET /songs/new.json | |
def new | |
@song = current_user.songs.build | |
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 user_song_path(@song.user, @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]) | |
if user_song_url.song.update_attributes(params[:song]) | |
redirect_to @song, notice: 'Song was successfully updated.' | |
else | |
render "edit" | |
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 user_song_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 user_song_url, notice: 'Liked!' } | |
format.json { head :ok } | |
else | |
format.html { redirect_to user_song_url, notice: 'Something went wrong...' } | |
format.json { render json: user_song_url.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 user_song_url, notice: 'Unliked!'} | |
format.json { head :ok } | |
else | |
format.html { redirect_to user_song_url, notice: 'Something went wrong...' } | |
format.json { render json: user_song_url.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment