I am a little confused with the routes in Rails 3 as I am just starting to learn the language. I have a form generated here:
<%= form_tag towns_path, :method => "get" do %>
<%= label_tag :name, "Search for:" %>
<%= text_field_tag :name, params[:name] %>
<%= submit_tag "Search" %>
<% end %>
Then in my routes:
get "towns/autocomplete_town_name"
get "home/autocomplete_town_name"
match 'towns' => 'towns#index'
match 'towns/:name' => 'towns#index'
resources :towns, :module => "town"
resources :businesses, :module => "business"
root :to => "home#index"
So why when submitting the form do I get the URL:
/towns?utf8=✓&name=townname&commit=Search
So the question is how do I make that url into a clean url like:
/towns/townname
Thanks,
Andrew
Firstly the routes
resources :towns do
post 'townname', :on => :collection
end
or
match "town/:name" => "towns#index", :as => :townname, :via => [:post], :constraints => { :name => /[A-Za-z]/ }
and the form
<%= form_tag townname_towns_path, :method => "post" do %>
<%= label_tag :name, "Search for:" %>
<%= text_field_tag :name, params[:name] %>
<%= submit_tag "Search" %>
<% end %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With