Server-side Image Maps in Rails

I’ve been working on an idea to use a server-side image map to allow
users to click anywhere in an area and have a new element appear at the
location they clicked. Everything was going well, until it came time to
connect the routing map. Here are the relevant parts:

In the view, the map is created by –

<%= link_to(image_tag(‘floor_plan.png’, :width => ‘1000’, :ismap =>
‘true’, :style => ‘position: absolute; top: 150px;’), :action =>
‘generate_ticket’) %>

A click anywhere on the map will generate a url of the form
“/report/generate_ticket?287,374” where 287 is the x-coord and 374 is
the y-coord. So in the routes.rb file we have (at the top of the list)

map.connect ‘:controller/:action?:x,:y’

Now, if I drop into the console, load up the routing map, and evaluate,
this is what I get –

rs = ActionController::Routing::Routes
=> #<ActionController::Routing::RouteSet:0x197da14
@routes_by_controller=nil, …>>

rs.recognize_path ‘/report/generate_ticket?10,20’
=> {:action=>“generate_ticket”, :controller=>“report”, :y=>“20”,
:x=>“10”}

However, when I click on the image map, the params that are returned to
report#generate_ticket are –

{“287,374”=>nil, “action”=>“generate_ticket”, “controller”=>“report”}

Furthermore, if I remove all routes except for the one that should
handle the image map coordinates, then click on the map, I get –

no route found to match “/report/generate_ticket” with {:method=>:get}

and I get the same error if I type the URL in manually. What gives?

Joshua B. wrote:

map.connect ‘:controller/:action?:x,:y’

I don’t think having the comma in the route is a terribly good idea.

Why not just use params?

maps/index?x=123&y=456

Douglas S. wrote:

Joshua B. wrote:

map.connect ‘:controller/:action?:x,:y’

I don’t think having the comma in the route is a terribly good idea.

What makes you say that? As far as I can tell, the
ActionController::Routing::SEPARATORS constant for Rails 1.2.3 is: ["/",
“;”, “.”, “,”, “?”]. I can’t find any reason why “?” and “,” shouldn’t
function as well as “/” (though, admittedly it appears that in my
situation they do not).

Why not just use params?

maps/index?x=123&y=456

I’d like to, but unfortunately setting the element “ismap” to true
dictates the behavior of the image map and you always end up with
“/something/stuff?###,###”. If you know of any way to communicate the
xy-coords back to the server in a different way, I’m all ears!

Oh, by the way, I’m trying this on Mac OS X using the WebKit nightlies
and Camino 1.4, Rails 1.2.3 and Ruby 1.8.6.

Thanks for the feedback!

Cheers,
Josh

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs