Forum: Ruby on Rails Newbie: from the javascript way to the Rails way... Advice?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ddc0d54f149d72fdaae41962ed62b2f5?d=identicon&s=25 Bill Spornitz (sporb)
on 2006-01-20 21:37
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
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (olbrich)
on 2006-01-20 22:09
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

or something similar.

_Kevin
25e11a00a89683f7e01e425a1a6e305c?d=identicon&s=25 Wilson Bilkovich (Guest)
on 2006-01-20 22:28
(Received via mailing list)
On 1/20/06, Kevin Olbrich <kevin.olbrich@duke.edu> 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
>
> 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
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.
Ddc0d54f149d72fdaae41962ed62b2f5?d=identicon&s=25 Bill Spornitz (sporb)
on 2006-01-20 23:39
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 <a href> tag in the html document, but I need to append to the
url in this <a href> 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
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (olbrich)
on 2006-01-20 23:58
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 <a href> tag in the html document, but I need to append to the
> url in this <a href> 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
This topic is locked and can not be replied to.