# Newbie: from the javascript way to the Rails way... Advice?

**URL:** <https://www.ruby-forum.com/t/newbie-from-the-javascript-way-to-the-rails-way-advice/52942>\
**Category:** Rails\
**Created:** [January 20, 2006, 9:37pm UTC](https://www.ruby-forum.com/t/newbie-from-the-javascript-way-to-the-rails-way-advice/52942 "2006-01-20T21:37:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sporb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9e8a1a/32.png) [@sporb](https://www.ruby-forum.com/u/sporb)\
**Post date:** [January 20, 2006, 9:37pm UTC](https://www.ruby-forum.com/t/newbie-from-the-javascript-way-to-the-rails-way-advice/52942/1 "2006-01-20T21:37:01Z")

</div>

Hello;

I’m building an application for a touchscreen that pages through a set  
of survey pages. On each page, the user makes some choices by touching  
items on the screen (words, or images) and, when satisfied with their  
choices, clicks the Next page button.

Rails has done a great job helping me get things to the screen, and  
paginating for me… but I’m stuck on something that I’ve implemented  
before in plain old javascript:

When a user touches an item, in the old days I would simply write a  
string to the end of the window.location.href:

function addThisToo(key, value)  
{  
window.location.href += “&” + key + “=” + value  
}

and, at some point later on, submit this, and there you go…

My experience with Rails, so far, has been that there are remarkably  
sensible solutions for many of web work’s little problems… is there a  
Rails solution for this one? (My preference is for a non-AJAX solution  
at this point…)

thanks to all

-Bill

---

<div class="post-metadata">

**Author:** ![sporb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9e8a1a/32.png) [@sporb](https://www.ruby-forum.com/u/sporb)\
**Post date:** [January 20, 2006, 10:09pm UTC](https://www.ruby-forum.com/t/newbie-from-the-javascript-way-to-the-rails-way-advice/52942/2 "2006-01-20T22:09:02Z")

</div>

Bill Spornitz wrote:

> Hello;
> 
> I’m building an application for a touchscreen that pages through a set  
> of survey pages. On each page, the user makes some choices by touching  
> items on the screen (words, or images) and, when satisfied with their  
> choices, clicks the Next page button.
> 
> Rails has done a great job helping me get things to the screen, and  
> paginating for me… but I’m stuck on something that I’ve implemented  
> before in plain old javascript:
> 
> When a user touches an item, in the old days I would simply write a  
> string to the end of the window.location.href:
> 
> function addThisToo(key, value)  
> {  
> window.location.href += “&” + key + “=” + value  
> }
> 
> and, at some point later on, submit this, and there you go…
> 
> My experience with Rails, so far, has been that there are remarkably  
> sensible solutions for many of web work’s little problems… is there a  
> Rails solution for this one? (My preference is for a non-AJAX solution  
> at this point…)
> 
> thanks to all
> 
> -Bill

You can add custom parameters to links like this…

link\_to “Do Not Push!”, {:controller=\>‘world’, :action=\>‘rotation’,  
:speed=\>“stop”}, :post=\>true, :confirm=\>“Are you sure? There is no  
Undo!”

This will map to

[http://www.armageddon.com/world/rotation/?speed=stop](http://www.armageddon.com/world/rotation/?speed=stop)

or something similar.

\_Kevin

---

<div class="post-metadata">

**Author:** ![sporb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9e8a1a/32.png) [@sporb](https://www.ruby-forum.com/u/sporb)\
**Post date:** [January 20, 2006, 10:28pm UTC](https://www.ruby-forum.com/t/newbie-from-the-javascript-way-to-the-rails-way-advice/52942/3 "2006-01-20T22:28:45Z")

</div>

On 1/20/06, Kevin O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > before in plain old javascript:
> 
> link\_to “Do Not Push!”, {:controller=\>‘world’, :action=\>‘rotation’,  
> :speed=\>“stop”}, :post=\>true, :confirm=\>“Are you sure? There is no  
> Undo!”
> 
> This will map to
> 
> [http://www.armageddon.com/world/rotation/?speed=stop](http://www.armageddon.com/world/rotation/?speed=stop)
> 
> or something similar.

In addition to this (good) suggestion, you can use named routes and  
routing variables to make this cleaner.  
[http://wiki.rubyonrails.org/rails/pages/NamedRoutes](http://wiki.rubyonrails.org/rails/pages/NamedRoutes)  
The above would then be:  
link\_to “Do Not Push”, stop\_button\_url(:speed =\> ‘stop’, :post =\>  
true, :confirm =\> “Are you sure?”)  
…which lets you omit the :action and :controller options.

---

<div class="post-metadata">

**Author:** ![sporb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9e8a1a/32.png) [@sporb](https://www.ruby-forum.com/u/sporb)\
**Post date:** [January 20, 2006, 11:39pm UTC](https://www.ruby-forum.com/t/newbie-from-the-javascript-way-to-the-rails-way-advice/52942/4 "2006-01-20T23:39:48Z")

</div>

Thank you for these excellent responses!

The main hurdle, as I see it, is that I need the browser to append stuff  
to the url at the client end; as I understand it, the link\_to function  
creates a tag in the html document, but I need to append to the  
url in this tag, based on user interaction, at the browser. The  
actual string appended is based on which selections the user makes. Am I  
seeing this incorrectly? Will link\_to accomodate this?

Thanks again!

-Bill

---

<div class="post-metadata">

**Author:** ![sporb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9e8a1a/32.png) [@sporb](https://www.ruby-forum.com/u/sporb)\
**Post date:** [January 20, 2006, 11:58pm UTC](https://www.ruby-forum.com/t/newbie-from-the-javascript-way-to-the-rails-way-advice/52942/5 "2006-01-20T23:58:48Z")

</div>

Bill Spornitz wrote:

> Thank you for these excellent responses!
> 
> The main hurdle, as I see it, is that I need the browser to append stuff  
> to the url at the client end; as I understand it, the link\_to function  
> creates a tag in the html document, but I need to append to the  
> url in this tag, based on user interaction, at the browser. The  
> actual string appended is based on which selections the user makes. Am I  
> seeing this incorrectly? Will link\_to accomodate this?
> 
> Thanks again!
> 
> -Bill

The link\_to will fix the link at render time. AJAX would be an easy way  
to do what you want, just have it observe the fields the user can change  
and have it update the link as needed.

\_Kevin
