Skip to content

Backend Training: Matching Route

Nelson Lee edited this page Dec 1, 2017 · 2 revisions

🔖 Matching Routes

We realize that when we refresh the page after submitting form it gives us this... contact#404

Let's investigate our routes.

$ rails routes | grep contacts
contacts POST       /contacts(.:format)       contacts#create
new_contact GET     /contacts/new(.:format)   contacts#new

Here we actually have a /contacts/ path but it is mapping to POST request. When we refresh the browser it sends a GET request to /contacts/ and we don't have a route for that.

Let's fix this by matching the routes so it won't break when visitors refresh the page.

# config/routes.rb
Rails.application.routes.draw do
  ......

  match 'contacts', to: 'contacts#new', via: %i[get]

  root 'home#index'
end

Refresh the browser Command ⌘+r, and great it maps to the new action!

contact#new

Let's commit and move on

$ git add -A
$ git commit -m 'Matching Contact Routes'

↪️ Next Section: Rails Ajax

Clone this wiki locally