What I want to do:
I'm creating a ruby on rails app, which possess a playlist. The playlist belongs to a user object, and is a hash. There are two crucial buttons that I need. One to add new music to the hash, and another to remove music from the hash. I'm using javascript to keep track of the current song, which whenever the add button is pressed it should be sent back to the add_song controller via ajax, which the string will then be added to the playlist hash. However I strongly believe my implementation is off because I keep getting a 500 error. Presently I'm only worried about the add_song button. Any help is well appreciated!
Present Error: POST http://localhost:3000/adding 500 (Internal Server Error)
Below is my unobtrusive javascript code:
var player = new Audio("File1.mp3");
var value = 1 ;
var track = "File.mp3";
function backward() {
var temp2 = player.src[player.src.length - 5] ;
if ( temp2 != 1){
value = parseInt(temp2);
value -= 1;
player.src = "/File"+value+".mp3";
player.play();
}
}
function forward() {
var temp2 = player.src[player.src.length - 5] ;
if ( temp2 != "5") {
value = parseInt(temp2);
value += 1;
player.src = "/File"+value + ".mp3";
player.play();
}
}
function play(){
player.src = "/File"+value + ".mp3";
player.play();
}
function pause( ) {
player.pause();
}
//add button
//delete button
function get_song() {
return player.src
}
$(document).ready(function() {
$("div div").click( function(){
if ( this.id == 'play' ) {
play();
}else if(this.id == 'backward'){
backward();
}else if(this.id == 'forward'){
forward();
}else if(this.id == 'stop') {
pause() ;
}else if(this.id == 'add_song') {
$.ajax({
url: "adding",
type: "post",
data: {data_value: JSON.stringify(player.src)}
});
}
});
});
Below is my staticpages controller
class StaticPagesController < ApplicationController
respond_to :js, :json, :html
def add_song()
if user_signed_in?
session[:user_id] = current_user.id
present_user = User.find(session[:user_id])
puts present_user
present_user.playlist.store(params[:data_value], 1)
present_user.save
puts "It works"
else
end
end
def remove_song()
if user_signed_in?
session[:user_id] = current_user.id
present_user = User.find(session[:user_id])
present_user.playlist.delete(params[:data_value])
present_user.save
else
end
end
def home
end
end
Lastly below is my routes:
devise_for :users, :controllers => { :sessions => "sessions" , :registrations => "registrations"}
devise_scope :user do
get "/log_in" => "sessions#new" ,as: :new_user_sessions
post '/log_in' => "sessions#create" ,as: :user_sessions
delete "/destroy" => "devise/sessions#destroy", as: :destroy_user
get '/edit' => 'devise/registrations#edit'
get "/sign_up" => "registrations#new", as: :new_user_registrations
post "/sign_up" => "registrations#create", as: :user_registrations
end
root 'static_pages#home'
match '/home', to:'static_pages#home', via: 'get'
match '/adding', to: 'static_pages#add_song', via: 'post'
It works Completed 500 Internal Server Error in 71ms (ActiveRecord: 1.1ms) ActionView::MissingTemplate (Missing template static_pages/add_song, application/add_song with {:locale=>[:en], :formats=>[:html, :text, :js, :css, :ics, :csv, :vcf, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip, :web_console_v2], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}add_song?adding POST /adding(.:format) static_pages#add_songapp/controllers/static_pages_controller.rb- the add_song method is what's needed.