Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined method 'patch' - routing error

I am beginner in Rails & MVC development, and need help with the below:

I am doing the example in http://edgeguides.rubyonrails.org/getting_started.html

In "5.12 Updating Posts", we are asked to add the following to our config/routes.rb:

patch "posts/:id" => "posts#update"

If I do that, and run rake routes, I am getting the below error:

undefined method `patch' for #<ActionDispatch::Routing::Mapper:0x390f078>

I get the same error when I go to - http://localhost:3000/posts/1

This is the line in edit.html.erb :

<%= form_for :post, url: { action: :update, id: @post.id }, method: :patch do |f| %>

I have Rails 3.2.1.

Environment:

I am doing this in Windows 7. I installed Rails via railsinstaller.org. Browsers - Chrome, Firefox

like image 690
user637563 Avatar asked Dec 31 '25 10:12

user637563


2 Answers

patch is only available in Rails master branch.

  • Here is the related pull request: https://github.com/rails/rails/issues/348
  • Here is the blog article explaining the reasoning: http://weblog.rubyonrails.org/2012/2/25/edge-rails-patch-is-the-new-primary-http-method-for-updates/
  • Here is another great summary: http://blog.remarkablelabs.com/2012/12/http-patch-verb-rails-4-countdown-to-2013

Among other things, you need to point to the git repo in your Gemfile to use edge rails on an already-existing project.

gem 'rails', :git => 'git://github.com/rails/rails.git'

For now you should just use PUT instead of PATCH. Even when 4.0 comes out, PUT isn't going anywhere.

like image 88
deefour Avatar answered Jan 02 '26 01:01

deefour


fwiw, I backported the HTTP PATCH verb work for Rails 3.2 https://gist.github.com/bf4/8940203

like image 31
BF4 Avatar answered Jan 02 '26 03:01

BF4