# Link via image map

**URL:** <https://www.ruby-forum.com/t/link-via-image-map/133543>\
**Category:** Rails\
**Created:** [April 1, 2008, 1:58pm UTC](https://www.ruby-forum.com/t/link-via-image-map/133543 "2008-04-01T13:58:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![reggieb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3e96dc/32.png) [@reggieb](https://www.ruby-forum.com/u/reggieb)\
**Post date:** [April 1, 2008, 1:58pm UTC](https://www.ruby-forum.com/t/link-via-image-map/133543/1 "2008-04-01T13:58:47Z")

</div>

I’m looking to create a small icon to use in table headings, that users  
can use to sort by that table heading. Click on the top half and sort  
ascending, and click on the bottom half to sort desc.

Is there a way to neatly generate an image map via Rails. I want to  
create output HTML something like this:

![]()

I can build my own helper method to do this, but wondered if there was  
already a way to do it.

---

<div class="post-metadata">

**Author:** ![reggieb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3e96dc/32.png) [@reggieb](https://www.ruby-forum.com/u/reggieb)\
**Post date:** [April 3, 2008, 2:47pm UTC](https://www.ruby-forum.com/t/link-via-image-map/133543/2 "2008-04-03T14:47:30Z")

</div>

As I’ve had no reply, I guess the answer is no - there isn’t an existing  
method. I’ve thrown these helper methods together which does the job for  
me:

def image\_map\_for\_sort\_link(name, field)  
params\_for\_link = params.clone  
params\_for\_link.delete\_if{|key,value| key == ‘order’ or key ==  
‘sort\_by’}  
output = Array.new  
output \<\< [’’].join  
output \<\< image\_map\_area(“0,0,20,18”, url\_for(:sort\_by =\> field,  
:params =\> params\_for\_link, :order =\> ‘asc’))  
output \<\< image\_map\_area(“20,0,40,18”, url\_for(:sort\_by =\> field,  
:params =\> params\_for\_link, :order =\> ‘desc’))  
output \<\< ‘’  
return output.join("\n")  
end

def sort\_image(field="")  
map\_name = “sort\_#{field}”  
@image\_maps = Array.new unless @image\_maps  
@image\_maps \<\< image\_map\_for\_sort\_link(map\_name, field)  
image\_tag(‘sort.png’, :usemap =\> “##{map\_name}”, :class =\>  
‘image\_map’)  
end

private  
def image\_map\_area(coords, url)  
[’’].join  
end

The result is that adding \<%= sort\_image(‘field\_name’) -%\> to a view  
adds a clickable sort image, as long as I also have \<%= @image\_maps %\>  
somewhere on the page. I also have to handle params[:sort\_by] and  
params[:order] in the controller to handle the sorting.
