Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I setup a simple todo app using AngularJS to test out its functionality, and once I got the basics running I decided I wanted to add persistence to the data.

I decided to setup a local environment using MySQL and to give things a bit of a twist I decided to use Ruby instead of PHP for server access.

The problem comes from executing the Ruby script and retrieving the data. Right now, when I try to use $http in AngularJS all it does is return the ruby script, not execute it.

Here's relevant code for reference.

todo.js:

var module = angular.module('app', []);

module.service('MySQLConnect', function($http) {
    this.getData = function() {
    $http.get('api/todos.rb').success(function(data) {
        console.log(data);
    });
    return response;
    }
});

module.controller('TodoCtrl', function($scope, MySQLConnect) {
    $scope.todos = MySQLConnect.getData();
});

todos.rb:

require 'mysql'

begin
    connection = Mysql.new 'localhost', 'root', 'root', 'todo', 8889, '/Applications/MAMP/tmp/mysql/mysql.sock'
    results = connection.query("SELECT * FROM todos")
    todos = []

    results.each_hash do |row|
        todos.push(row)
    end

    todos

rescue Mysql::Error => e
    puts e.errno
    puts e.error

ensure
    connection.close if connection
end

Right now, the console log for data is only returning everything written in todos.rb. How can I get it to actually execute the script and return the todos array?

share|improve this question
    
The ruby code needs to be behind a server that knows to invoke it as a ruby script instead of just returning it as plain text. If you're using plain local URLs, there's no server and it just won't work. –  Pointy Jun 26 at 20:58
    
If Pointy is correct, you can test it real fast. Open up the command line, go to the directory where todo.js is and and start a python server ("python -m SimpleHTTPServer 8000"). Then direct your browser to localhost:8000 –  Josh Bradley Jun 26 at 21:07
    
@pointy, I think that may be the issue. As this was meant to be just a simple toy app I didn't think about configuration. I'm currently using MAMP for my IDE, and I don't believe Apache supports Ruby. Know of a way to configure Apache to use Ruby or another IDE which supports Ruby and MySQL? –  Ryan B Jun 27 at 13:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.