views:

28

answers:

2

Hi,

I'd like my Search form to return the following url after submit:

/anuncios/buscar/the_text_I_searched

My form is the following:

<% form_for :announcement, :url => search_path(:txtSearch) do |f| %>
<div class="searchBox" id="basic">
  <%= text_field_tag :txtSearch, params[:str_search].blank? ? "Busc&aacute; tu curso r&aacute;pido y f&aacute;cil." : params[:str_search], :maxlength=> 100, :class => "basicSearch_inputField", :onfocus => "if (this.value=='Busc&aacute; tu curso r&aacute;pido y f&aacute;cil.') this.value=''", :onblur => "if(this.value=='') { this.value='Busc&aacute; tu curso r&aacute;pido y f&aacute;cil.'; return false; }" %>
  <div class="basicSearch_button">
    <input type="submit" value="BUSCAR" class="basicSearch_buttonButton" />
    <br /><a href="#" onclick="javascript:jQuery('#advance').modal({opacity:60});">Busqueda avanzada</a>
  </div>
</div>
<% end %>

My routes' line for search_path is this:

map.search '/anuncios/buscar/:str_search', :controller => 'announcements', :action => 'search'

Well, this will work if I manually type the url I want in the brower, but definitely, if you look at the form's url, you'll find a ":txtSearch" parameter, which is not giving me the actual value of the text field when the form is submitted. And that's what I'd like to get!

Could anybody help me on this?

+2  A: 

You should be able to do this in the controller:

def search
  ...
  redirect_to search_path(:str_search => params[:txtSearch])
end
John Topley
This works. Now I've got a question...I noticed that if the querystring param is null, let's say I type "/anuncios/buscar" (without any string to search) it will raise an error because it cannot find the action to execute. What can I do to catch this action?(I'd like to show all the announcements if no string to search is specified in the url).
Brian Roisentul
Check `params[:txtSearch].blank?` and render the page containing the search form if it is, otherwise do the redirect.
John Topley
+1  A: 

Sounds like you'll need to submit the form to another action, format the search string, and then redirect_to your search page using the new search string.

Jarrod