Using form_tag with a get request with params

I’m trying to create a search field using the form_tag helper. The
problem is when I enter a search, the url_for returns an address such as
host.com/search?term=foo. I want to use the format
host.com/search/term. I have it set up in routes to recognize the
format search/:term, but form_tag always uses the format
host.com/search?term=foo. Does anybody know how to do this?

Cheers!
Ray

all I can think of is that order of routes matters, and there may be a
conflict with the default :controller:action scheme. try moving it up
the
list, but be careful that it doesn’t break the rest of your routes.

On 12/11/06, Raymond O’connor [email protected] wrote:

Ray


Posted via http://www.ruby-forum.com/.


Mike W.
Web D.
UW-Eau Claire

I have the route at the top of the file and it still doesn’t work. Here
is the line from my routing file:

map.connnect ‘products/search/:term’,
:conditions => { :method => :get },
:controller => ‘products’,
:action => ‘search’

and here is the rhtml where I display the search bar:

<% form_tag({:controller => ‘products’, :action => ‘search’}, {:method
=> ‘get’}) do %>
<%= text_field_tag :term, params[:term] %>

<% end %>

When I search using this bar, I always get addresses like this:
http://localhost:3004/products/search?term=hello. Anything look wrong
with the above? Thanks for the help!

I need 2 display the content of textbox in a html page to another

Well, besides connect being spelt with three n’s I don’ think that
there is anything wrong with your code. And rails is behaving exactly
as expected too.
When the form is created, I believe that rails will generate html like
this (it did when I looked at your code);

So, "term" is supposed to vary depending upon what the user keys in but then "term" is also supposed to appear into the html before the user types in anything. I don't think that there is much change of that working. The URL generated from the form is fine. http://localhost:3004/products/search?term=something is correct.

The routing is working. You can see this if add some code to the
search.rthml view that displays the value of the params[:term]. Also a
URL like http://localhost:3004/products/search/here-is-my-term works.
params[:term] is set to “here-is-my-term” in the search method of the
controller. When the user then keys in a value into the form and
clicks the submit button, the URL becomes
http://localhost:3004/products/search/here-is-my-term?term=

Maybe dropping the route and just letting the form do it’s thing is
the simplest way forward?

regards,
Bruce.

Have a google for the term ‘scraping a web page’. That’s what you’re
trying to do, unless of course that “other” html page is on your server
already.

Walter