Archive for the ‘ruby on rails’ Category

Error messages chugging along

Tuesday, January 15th, 2008

Getting nice looking error messages isn’t easy. It’s easy to use the Rails default (”error_messages_for”) but for the type of errors you’d ideally want you’ll still have to do a little leg work. One nice part it that any form fields that are being output using the Rails form helpers will be wrapped in a div with the “fieldWithErrors” class. This makes adding a red border to problem fields extremely simple.

Also, with the help of the recent post about multiple selects over on railscasts, I was able to add in my country/select fields in a very streamlined way. I’m using the same “address” partial for users and arcades at the moment as well, with that partial including the javascript in the header. Pretty slick just including a partial and having all the javascript just fall into place. For now adding a single arcade is working fine all around, although i’m a little concerned with how my nested resources are starting to become cluttered.

Nested resources are just what they sound like. For instance if you wanted to get a users address, you might go to the url http://localhost:3000/users/:user_id/address/1. That’s a very useful looking URL that you can find a lot from. The downside is that the controller/action this will hit is (by default) address_controller.show. This is the exact same controller/action that is hit when you go to http://localhost:3000/address/1 though! The only difference is that there will be a “user_id” variable passed in. After that though you might handle little things differently like the title, the edit link for that resource, and maybe even the entire page. Aparently you can make this resource go to another controller, like “user_address_controller” for example and not have to worry about this. I’m starting to think this is the way to go for a lot of this.

YM4R and GeoKit Rock

Sunday, December 23rd, 2007

So far YM4R and GeoKit are working out aside from a few oddities. GeoKit install and geocoding was flawless, but I’ve yet to use the advanced finders for distance. At the moment the user/gamer/player (haven’t decided what to call it as of yet) doesn’t have an address associated with it for mapping functionality. Should be easy enough to compute according to tutorials.

YM4R makes the simple things even easier, but due to the very small amount of documentation its been a bit difficult to find examples. It’s probably time to start digging through the plugin code for clarification. YM4R is used for creating your Google Map markers in Ruby code, storing them to single variable and then being able to generate the header javascript, as well as the div/span/whatever the map will be created in. It’s easy enough to wrap some code in a content tag from within a view for a specific map.

<% content_for(:header) do %>
<%= GMap.header %>
<%= javascript_include_tag("markerGroup") %>
<%= @map.to_html %>
<% end %>

The @map variable was created within the controller with all points that will be created, as well as any map settings — like zoom level, lat/long to center in on, as well as managing groups, clusters and anything else. When generating a map with a group of points, it’s possible to have ym4r decide where to zoom in on, as well as what zoom level. This is as easy as a few lines:

sorted_latitudes = @arcades.collect(&:address).collect(&:lat).compact.sort
sorted_longitudes = @arcades.collect(&:address).collect(&:lng).compact.sort
@map.center_zoom_on_bounds_init([ [sorted_latitudes.first, sorted_longitudes.first],
[sorted_latitudes.last, sorted_longitudes.last]])

I’m still working on some of the basics of mapping that aren’t quite as well documented. Things like having a marker point popup on start, or having a link on the page outside of the map that opens a map marker. These kinds of things are simple when you step into javascript, and heavily documented, but not quite as much so with wrapper plugins. I’ll probably mess around with it a bit more, before deciding if straight javascript or ruby is the way to go.

As far as javascript goes, one solution that looks interesting is Mapstraction. There is actually a YM4R wrapper for it as well, although it appears out of date (and undownloadable). The idea is that you can switch which mapping engine you use. Always a plus to give users the ability to decide which mapping engine they prefer, but I imagine it’s at the cost of some features. Google, for instance has quite a few different ways of grouping points so the browser doesn’t crash to a halt. For instance, if you use clustering (which the ym4r gm plugin supports), markers that indistinguishable at far out zoom level will be shown as a single point. This keeps the map clean so that you won’t have a collection of points all on top of each other. We’ll see how it goes with the remaining tasks!

Mapping Goodness!

Tuesday, December 18th, 2007

I’ve been a longtime fan of mapping applications — making that one of the most fun aspects of ArcadeFly. It’s a feature I’m not going to wimp out on in any way, so long as I have more ideas on what to do. To start out, I’m using GeoKit to obtain latitude/longitude for addresses, which has been the easiest geocoding I’ve ever had the opportunity to work with. Basically you just add a callback in your model, add in a lat/lng column to wherever your address is, update your environment file with your API key and you’re good. Worked on the first try actually, which is rare. One of the bonus features of is that it can use multiple mapping servers to lookup an address until it finds one that works. Pretty slick feature and that much more I don’t have to worry about. I’m working on displaying the results now, which I’m thinking about using YM4R. We’ll see how that goes.