YM4R ActionView::TemplateError and Related Errata

Ruby on Rails 4 Comments »

Using Guilhem Vellut’s brilliant YM4R plugin for Google map and other mapping mashups you might run into the following issue:

ActionView::TemplateError (yourdomain.com) on line #3 of app/views/google_map
s/show.html.erb:
1: <% content_for :head do %>
2:
3:  <%= GMap.header(:with_vml => false) %>
4:   <%= @map.to_html %>
5: <style type=”text/css”>
6:   #map_div {height:500px;}

    /var/www/apps/yourapp/releases/20090210175842/vendor/plugins/ym4r_gm/l
ib/gm_plugin/key.rb:28:in `get’

Another related error might be a Ym4r::GmPlugin::AmbiguousGMapsAPIKeyException, which is actually the underlying error of the above issue, called in the quoted line 28 of ym4r_gm/lib/gm_plugin/key.rb.

The cause of the problem is that you call

<%= GMap.header(:with_vml => false) %>

in your view instead of

<%= GMap.header(:with_vml => false, :host => request.host) %>

Yep, it’s that simple. As soon as you have multiple hosts configured in your gmaps_api_key.yml file, you need to specify the host option. Guilhelm’s comment in the code (line 23 & 24 of key.rb):

#For this environment, multiple hosts are possible.
#:host must have been passed as option

To be honest, the host could have been defaulted to the current request’s host in the plugin, but as things stand :host => <some_host> has to be passed.

Now you can also do sth. like this in your :gmaps_api_key.yml file, for example if you have several apps running on phusion passenger in development as discussed in this railscast:

development:
 localhost: <api key> 
 yourdomain.local: <api key>

test: <api key>

production: 
 yourdomain1.com: <api key>
 yourdomain2.com: <api key>

Hope it helps.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Paginate over HABTM with Globalize2

Ruby on Rails No Comments »

As outlined in this post, find and paginate require a :joins => :globalize_translations statement to eager load the translations.

Here’s an extended version for pagination over a has-and-belongs-to-many (HABTM) relationship:

#app/models/widget.rb

has_and_belongs_to_many :categories

#…

def self.find_by_category(category_id, page)
      paginate(
      :page => page,
      :per_page => 30,
      #:include => :categories, #does not work but this does:
      :joins => [:categories, :globalize_translations],
      :conditions => ['category_id = ? and locale = ?', category_id, 'en'],
      :order => :name,
      :include => :pictures
     
      )
    end

I couldn’t get  :include => :categories to work, it had to be a :joins => :categories. Coming from the paginated object (in this case Widget) instead of from the related Category lets you eager-load other related models (in this case Pictures) very easily. YMMV.

 

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]
WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in