Tag Archives: rails

Saving Omniauth Provider Data in the Database

I spent some time searching for a way to not need the App ID and Secret Key for OmniAuth in an initializer. Luckily I ran across a post mentioning this link:


By following these instructions, you can wait and set the App ID and Secret key at request time. In my case, I’m going to pull it out of a database table which stores config items.

My Current Code looks like this:

unless Rails.env.nil?
  CONFIG = YAML.load_file(Rails.root.join("config/secrets.yml"))[Rails.env]

  Rails.application.config.middleware.use OmniAuth::Builder do
    provider :facebook, CONFIG['fb_app_id'], CONFIG['fb_secret_key'], :scope => 'email,user_about_me,user_activities,user_birthday,user_groups', :display => 'popup'

And the future code will look like this:

SETUP_PROC = lambda do |env| 
  config = Config.find(:first)
  env['omniauth.strategy'].options[:consumer_key] = config.facebook_key
  env['omniauth.strategy'].options[:consumer_secret] = config.facebook_secret
use OmniAuth::Builder.new do
  provider :facebook, :setup => SETUP_PROC

Tagged , , ,

How to get domain objects in javascript with rails (Part 1)

In continuing with my side project relating to maps and restaurants, I wanted to get things a little more interactive.  The next step is to combine all my restaurants I have the in database, and put them on the map.  To do this though, I needed to get the Restaurant Model objects into javascript so I could dynamically add them to as markers on the map.

The first step was to define a new controller action in my “app/controllers/restaurants_controller.rb” file.  For now, this will just find the first item in the DB and render it as a JSON object.  In the long run, this will return a list of recommended restaurants:

  def recommendations
    @restaurant = Restaurant.find(:first)
    render :json => @restaurant

Next, I needed to make that controller accessible via a URL.  My initial idea is to open up the URL like “/restaurants/recommendations” to return the list.  I modified the “config/routes.rb” file, and restarted the server:

  match "/restaurants/recommendations" => "restaurants#recommendations"

If I point the browser to this URL, I see the JSON I was expecting to see.  In this case, it is returning the restaurant “Watts Grocery”:

"name":"Watts Grocery",

Next up, I’ll show how to make a call in javascript to get this JSON string and parse it into an object so it can be displayed on the map.


Tagged , , ,

Playing with OpenStreetMap and Cloudmade

I’ve been doing some research into using maps in a web application, and I wanted to check out the different providers that are available.  Since hearing that Apple was moving away from the Google Maps API in iPhoto, I was curious to give it a try myself.

Since OpenStreetMap is a “free” alternative for mapping data, I looked around and CloudMade.com was the first provider with an API that I was able see an easy way to use.  It works very similar to Google Maps, and has a similar javascript API.

Here’s how I added it to a simple Rails 3 app…

First, I created a new controller “app/controllers/maps_controller.rb” and put an index function in:

class MapsController < ApplicationController
  def index

Next, I created a separate javascript doc “app/assets/javascript/maps.js”:

$(document).ready(function(){ /*code here*/ 
    var cloudmade = new CM.Tiles.CloudMade.Web({key: 'my_key'});
    var map = new CM.Map('map_canvas', cloudmade);

     var geocoder = new CM.Geocoder('my_key');
    geocoder.getLocations('Durham, NC', function(response) {
      var southWest = new CM.LatLng(response.bounds[0][0], response.bounds[0][1]),
	      northEast = new CM.LatLng(response.bounds[1][0], response.bounds[1][1]);
      map.zoomToBounds(new CM.LatLngBounds(southWest, northEast));
    map.addControl(new CM.LargeMapControl());

Next, I created the maps index view “app/views/maps/index.html.rb” with a link to the open maps javascript library and the maps javascript doc:

<% content_for(:head) do %>
<%= javascript_include_tag "http://tile.cloudmade.com/wml/latest/web-maps-lite.js", :maps %>
<% end %>

  user = <%= current_user %>

<br />
<br />
<div id="map_sidebar">Top Restaurants </div>
<div id="map_canvas"></div>

Finally, modify the routes.rb file to allow localhost:8080/maps to be directed to my new controller and view:

  match "/maps" => "maps#index"

Add this all up and we get a nice map showing on our page:

The only weird thing I noticed is that there are some issues when zooming in.  After a certain point, the tiles stop loading:

If you’re interested in learning more, the CloudMade tutorials show pretty much how to do everything in easy steps here.

Tagged , , , , , ,