In Rails, how do I query my database from JavaScript so that I can fill in a textbox?

I have a view that uses the select method in Ruby on Rails to return the following code:

    <select class="form-control" name="xyz" id="food_id">
    <option value="1">Peach</option>
    <option value="2">Apple</option>
    <option value="3">Orange</option>
    </select>

To pass this information over to the JavaScript file, I am basically using a link_to method in my view, such as the following:

<%= link_to "Import text", "#/", :onclick => "fillTextBox()" %>

In the JS file, how can I basically get the name and description of whatever value is selected in the dropdown? This is as far as I’ve gotten before I got confused:

function fillTextBox(){
    var dropdown = document.getElementById('food_id');
    var foodID = dropdown[dropdown.selectedIndex].value;
}

I need to do something like:

var foodName = Food.find(foodID).name;
var foodDescription = Food.find(foodID).description;

Is this possible? The rest of the JS function would basically fill in a textbox using foodName and foodDescription, but I first need to be able to query the database for this information in the first place.


Source: stackoverflow-javascript

How to trigger rails post method from faye.ru file (using faye)

so i am using faye pub-sub in my application, publish is happening from different application, in my faye.js i have written ajax post method for rails. now if 5 pages of my application is opened in browser, faye.js is loaded 5 times and post method is called 5 times. if not a single page is opened, post method wont work even once. however i am receiving published data in faye server. so is there a way of calling rails post method in faye.ru file when i use callback method.
This is my faye.ru

require 'faye'

require File.expand_path('../config/initializers/faye_token.rb', __FILE__)

Faye::WebSocket.load_adapter('thin')
Faye.logger = Logger.new(STDOUT)

class ServerAuth
  def incoming(message, callback)
    if message['channel'] !~ %r{^/meta/}
      if message['ext']['auth_token'] != ENV['FAYE_TOKEN']
        message['error'] = 'Invalid authentication token'
      end
    end
    callback.call(message)
  end

  # IMPORTANT: clear out the auth token so it is not leaked to the client
  def outgoing(message, callback)
    if message['ext'] && message['ext']['auth_token']
      message['ext'] = {} 
    end
    callback.call(message)
  end
end

$bayeux = Faye::RackAdapter.new(:mount => '/faye', :timeout => 25)

$bayeux.add_extension(ServerAuth.new)

run $bayeux

and my faye.js

$(function() {
  var faye = new Faye.Client('http://localhost:9292/faye');
faye.subscribe("/functional", function(data) {
    ajax_call(data);
  });
});


Source: stackoverflow-javascript

Choosing programming platform/language: MQTT, DB & Webserver

I want to write a server application which is able to subscribe to an MQTT broker, store the data it receives in a database like MySQL and also present the data in a webpage, but I don’t know what the best setup would be for this.

Options I thought of:

  1. Separate programs for all steps: One which handles the connection between the MQTT broker in Java or something and another framework in a framework I’m familiar with like RoR or PHP.

  2. A complete package written in NodeJS which does everything self contained.

Which of these is the most efficient and/or flexible approach, or am I completely missing the mark and do you have a better solution?


Source: stackoverflow-php