# Server-side Image Maps in Rails

**URL:** <https://www.ruby-forum.com/t/server-side-image-maps-in-rails/105773>\
**Category:** Rails\
**Created:** [June 2, 2007, 4:17pm UTC](https://www.ruby-forum.com/t/server-side-image-maps-in-rails/105773 "2007-06-02T16:17:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jballanc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/97f17d/32.png) [@jballanc](https://www.ruby-forum.com/u/jballanc)\
**Post date:** [June 2, 2007, 4:17pm UTC](https://www.ruby-forum.com/t/server-side-image-maps-in-rails/105773/1 "2007-06-02T16:17:07Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jballanc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/97f17d/32.png) [@jballanc](https://www.ruby-forum.com/u/jballanc)\
**Post date:** [June 2, 2007, 10:57pm UTC](https://www.ruby-forum.com/t/server-side-image-maps-in-rails/105773/2 "2007-06-02T22:57:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jballanc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/97f17d/32.png) [@jballanc](https://www.ruby-forum.com/u/jballanc)\
**Post date:** [June 3, 2007, 1:55am UTC](https://www.ruby-forum.com/t/server-side-image-maps-in-rails/105773/3 "2007-06-03T01:55:58Z")

</div>

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
