开发者

Rails 3 routes and using GET to create clean URLs?

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开发者_JAVA百科 :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 %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜