# Outputting a rails url to javascript

**URL:** <https://www.ruby-forum.com/t/outputting-a-rails-url-to-javascript/191031>\
**Category:** Rails\
**Created:** [July 3, 2010, 2:07am UTC](https://www.ruby-forum.com/t/outputting-a-rails-url-to-javascript/191031 "2010-07-03T02:07:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tig](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/73ab20/32.png) [@Tig](https://www.ruby-forum.com/u/Tig)\
**Post date:** [July 3, 2010, 2:07am UTC](https://www.ruby-forum.com/t/outputting-a-rails-url-to-javascript/191031/1 "2010-07-03T02:07:53Z")

</div>

Hi,

I’m working on a rails/google maps app, where makers on the map show  
‘places’. I would like to be able to put a link into each marker’s  
infowindow that will take the user to the ‘show’ action for that place  
(ie. /places/[place\_id]), and am looking for ways to output the  
appropriate url so it can be incorporated into my /public/javascripts/  
map.js file. I’m using json to output the latititude, longitude, name  
etc of each place to the map…is there a way to extend this to urls?

Any help would be much appreciated, thanks in advance!

---

<div class="post-metadata">

**Author:** ![Tig](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/73ab20/32.png) [@Tig](https://www.ruby-forum.com/u/Tig)\
**Post date:** [July 3, 2010, 2:09am UTC](https://www.ruby-forum.com/t/outputting-a-rails-url-to-javascript/191031/2 "2010-07-03T02:09:39Z")

</div>

FYI: Rails 3beta4, Ruby 1.8.7 and using jQuery with my map.

---

<div class="post-metadata">

**Author:** ![Tig](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/73ab20/32.png) [@Tig](https://www.ruby-forum.com/u/Tig)\
**Post date:** [July 3, 2010, 7:54am UTC](https://www.ruby-forum.com/t/outputting-a-rails-url-to-javascript/191031/3 "2010-07-03T07:54:24Z")

</div>

Tig wrote:

> Hi,
> 
> I’m working on a rails/google maps app, where makers on the map show  
> ‘places’. I would like to be able to put a link into each marker’s  
> infowindow that will take the user to the ‘show’ action for that place  
> (ie. /places/[place\_id]), and am looking for ways to output the  
> appropriate url so it can be incorporated into my /public/javascripts/  
> map.js file. I’m using json to output the latititude, longitude, name  
> etc of each place to the map…is there a way to extend this to urls?

Of course! URLs are just strings. Stick 'em in the JSON packet with  
everything else.

> Any help would be much appreciated, thanks in advance!

---

<div class="post-metadata">

**Author:** ![Tig](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/73ab20/32.png) [@Tig](https://www.ruby-forum.com/u/Tig)\
**Post date:** [July 5, 2010, 11:42pm UTC](https://www.ruby-forum.com/t/outputting-a-rails-url-to-javascript/191031/4 "2010-07-05T23:42:52Z")

</div>

Awesome, I hoped it would be that easy! Thanks for your help

---

<div class="post-metadata">

**Author:** ![Tig](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/73ab20/32.png) [@Tig](https://www.ruby-forum.com/u/Tig)\
**Post date:** [July 8, 2010, 11:04pm UTC](https://www.ruby-forum.com/t/outputting-a-rails-url-to-javascript/191031/5 "2010-07-08T23:04:11Z")

</div>

I’ve found a solution - instead of adding a ‘link’ column to my  
‘places’ table, I can just use the place id value to create a link in  
my js file:

[Show more!](https://www.ruby-forum.com/places/"+place.id+")"

Hope that sounds okay, it seems to work fine!

---

<div class="post-metadata">

**Author:** ![Tig](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/73ab20/32.png) [@Tig](https://www.ruby-forum.com/u/Tig)\
**Post date:** [July 8, 2010, 2:28am UTC](https://www.ruby-forum.com/t/outputting-a-rails-url-to-javascript/191031/6 "2010-07-08T02:28:58Z")

</div>

Hi,

I’m struggling a bit to get an appropriate url to save - I’ve added a  
‘link’ column to my ‘places’ table, and am populating the hidden form  
field automatically using @place.link = places\_path(@place.id) in the  
‘new’ action of the places controller. This seems to output just /  
places however - am I going about this the wrong way? I’m also  
struggling to incorporate the /places link I’ve exported to JSON into  
my javascript code…thanks for your help!

---

<div class="post-metadata">

**Author:** ![Tig](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/73ab20/32.png) [@Tig](https://www.ruby-forum.com/u/Tig)\
**Post date:** [July 9, 2010, 10:24am UTC](https://www.ruby-forum.com/t/outputting-a-rails-url-to-javascript/191031/7 "2010-07-09T10:24:55Z")

</div>

A minor observation:  
I suppose you are taking care of nil objects here: place.id. Otherwise  
the link will crash if “place” object is nil. I would propose doing  
something like:

place\_id = place.id rescue “” # or you put your rescue sub-link here  
“[Show more!](https://www.ruby-forum.com/places/"+place_id+")”

Regards,

Ceekays.
