# Using routes with forms

**URL:** <https://www.ruby-forum.com/t/using-routes-with-forms/85696>\
**Category:** Rails\
**Created:** [February 15, 2007, 4:44pm UTC](https://www.ruby-forum.com/t/using-routes-with-forms/85696 "2007-02-15T16:44:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![spacechurro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d2c977/32.png) [@spacechurro](https://www.ruby-forum.com/u/spacechurro)\
**Post date:** [February 15, 2007, 4:44pm UTC](https://www.ruby-forum.com/t/using-routes-with-forms/85696/1 "2007-02-15T16:44:47Z")

</div>

i have a form that uses :method =\> ‘get’ to pass data to the  
controller…

the form has one text field called “zipcode”…

i also have a route that looks like:

map.connect ‘find\_place/:zipcode’, :controller =\> ‘find\_place’, :action  
=\> ‘by\_zip’

this will allow me to pass info on the url…

what i would like to do is have the form spit the “get” call so that it  
follows the routing and looks like:

/find\_place/90210

instead of:

/find\_place/by\_zip?zipcode=90210&commit=Find+a+Place%21

anyone have any ideas?

thanks!

---

<div class="post-metadata">

**Author:** ![spacechurro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d2c977/32.png) [@spacechurro](https://www.ruby-forum.com/u/spacechurro)\
**Post date:** [February 15, 2007, 5:19pm UTC](https://www.ruby-forum.com/t/using-routes-with-forms/85696/2 "2007-02-15T17:19:34Z")

</div>

That should work, but you need to have that route first, before any  
other route that will catch the params and build a URL.  
I would do  
map.connect ‘find\_place/:zipcode’, :controller =\> ‘find\_place’, :action  
=\> ‘by\_zip’, :zipcode =\> /[0-9]{5}/  
That will match your route only if the zip is a string of 5 digits.  
-Jason

---

<div class="post-metadata">

**Author:** ![spacechurro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d2c977/32.png) [@spacechurro](https://www.ruby-forum.com/u/spacechurro)\
**Post date:** [February 15, 2007, 7:49pm UTC](https://www.ruby-forum.com/t/using-routes-with-forms/85696/3 "2007-02-15T19:49:59Z")

</div>

Sergio R. wrote:

> i have a form that uses :method =\> ‘get’ to pass data to the  
> controller…  
> the form has one text field called “zipcode”…
> 
> i also have a route that looks like:  
> map.connect ‘find\_place/:zipcode’, :controller =\> ‘find\_place’, :action  
> =\> ‘by\_zip’
> 
> this will allow me to pass info on the url…  
> what i would like to do is have the form spit the “get” call so that it  
> follows the routing and looks like:
> 
> /find\_place/90210
> 
> instead of:  
> /find\_place/by\_zip?zipcode=90210&commit=Find+a+Place%21
> 
> anyone have any ideas?
> 
> thanks!

This won’t be possible without javascript. The fault lies with the  
browser. The knows that that a form posted via “get” needs to have the  
form data in the url. The the browser creates a query string out of the  
form data and appends it the form action url.

The browser can’t read your routes.rb file, and wouldn’t be able to  
parse it anyways. So the browser contructs the url in the only way it  
knows how.

There are two ways around this, one server side and one client side:

Server Side:  
You could detect the submission to the ugly url and redirect to the  
right one with something like:

def by\_zip  
if request.request\_uri =~ /?/  
return redirect\_to url\_for(:controller =\> ‘find\_place’,  
:action =\> ‘by\_zip’,  
:zip\_code =\> params[:zipcode])  
end

```
...

```

end

Client side:  
Use javascript to dynamically change what happens when the submit button  
is clicked.

\<%= submit\_button “Submit”,  
:onclick =\> “window.location.href =  
‘#{/find\_place/’+$(‘zip’).value” %\>

---

<div class="post-metadata">

**Author:** ![spacechurro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d2c977/32.png) [@spacechurro](https://www.ruby-forum.com/u/spacechurro)\
**Post date:** [February 15, 2007, 8:54pm UTC](https://www.ruby-forum.com/t/using-routes-with-forms/85696/4 "2007-02-15T20:54:51Z")

</div>

thanks so much!

i think i will go with the javascript version…

thanks again…
