ActionController::MethodNotAllowed (Only get and post requests are allowed.)

I have a restful controller that when a uri of items/1/edit is
displayed and then saved, I get that message.

The model is InventroyItem and the controller is
InventroyItemsController.
The routes.rb contains map.resources inventory_items
The form uses partials and contains:

<% form_for(:inventory_item, :url =>
inventory_item_path(@inventoryitem), :html => { :method => :put }) do |
f| %>
<%= render :partial => ‘inventory_item’, :locals => {:f => f} %>
<% end %>

Any ideas?

Donald F.

bump.

Any ideas, anybody?

Don F.

Don F. wrote:

I have a restful controller that when a uri of items/1/edit is
displayed and then saved, I get that message.

The model is InventroyItem and the controller is
InventroyItemsController.
The routes.rb contains map.resources inventory_items
The form uses partials and contains:

<% form_for(:inventory_item, :url =>
inventory_item_path(@inventoryitem), :html => { :method => :put }) do |
f| %>
<%= render :partial => ‘inventory_item’, :locals => {:f => f} %>
<% end %>

Any ideas?

Donald F.

Any solution… I have a similar problem. But the error i get is

“ActionController::MethodNotAllowed Only get requests are allowed.”

I had this problem a couple of months ago… and found a solution. But
for the life of me can’t remember what it was and where i saved it.

Ok… I solved the problem i was having. I had a typo in my form_for

this…

<% form_for @profile, :url=>person_profile_url, :html=>{:method=>:put}
do |f| %>

when it should have been this

<% form_for :profile, @profile, :url=>person_profile_url,
:html=>{:method=>:put} do |f| %>

Hope this helps someone.

Hi!

I also got this problem for a new app I am building. Whenever I try a
non-get AJAX request, I get :

Only get, put, and delete requests are allowed.

I have a config/routes.rb like this one:

map.namespace :admin do |admin|
admin.resources :surveys do |survey|
survey.resources :questions do |question|
question.resources :choices
end
end
end
map.resources :surveys # problem!

I noticed that commenting the last line and rebooting the server would
solve the problem. Well, of course I’ll eventually need this last
route as well. So by accident I think I discovered a workaround for
this (crossing my fingers…)

I uncommented the “map.resources :surveys” line, and hit the bug again
with something like this:

# GET /admin/surveys/344/questions/8113/edit # views/admin/questions/edit.html.erb <%= link_to_remote 'Delete', :url => admin_survey_question_choice_path(:question_id => params[:id], :id => choice), :method => :delete} %>

DELETE /admin/survey/344/questions/8113/choices/35788

controllers/admin/choice_controller.rb

def destroy
@choice = Choice.find_by_id(params[:id])
@choice.destroy
respond_to do |format|
format.js
format.xml { head :ok }
end
end

views/admin/choices/destroy.js.rjs

update_page do |page|
page.remove “choice_#{params[:id]}”
end

I then renamed the last file from destroy.js.rjs to destroy.js.erb
with the following code:

views/admin/choices/destroy.js.erb

<%
update_page do |page|
page.remove “choice_#{params[:id]}”
end
%>

And it worked !

So it looks like this is a bug with the RJSGenerator possibly combined
with Mongrel (1.1.5). Because the exception raises AFTER the view has
been rendered, and AFTER the last line of the action code in the
controller.

I am using Rails 2.1; and by the way I also tried to put this:
RAILS_GEM_VERSION = ‘2.0.2’ unless defined? RAILS_GEM_VERSION</
code>
in environment.rb. So this bug occurs with Rails 2.0.2 as well…

  • David

Did you ever solve this? I have the same problem and I have no idea
what’s causing it.

I experienced this problem with Rails 2.1 and an old version of mongrel.
The solution was to upgrade mongrel.

sudo gem update mongrel

This solved the problem.
gotcha++

Wee blog post about it:
http://scoop.cheerfactory.co.uk/2008/07/14/rails-gotcha-actioncontrollermethodnotallowed/

Which version of Mongrel do you have?

I am using 1.1.5, which I think is the latest.

2008/8/6 Alastair B. [email protected]

Hey I found something else…

It seems like when this bugs occurs (ie: when using an .RJS extension),
there is a double request made to (or by) the webserver. But at the
second request, all the form arguments are empty, which seems to cause
the _method=delete argument to be lost and raises the MethodNotAllowed
exception.

After erasing the log file and rebooting, here’s the log of the error:

Processing ChoicesController#destroy (for 127.0.0.1 at 2008-08-06
21:53:14) [DELETE]
Session ID:
BAh7BzoMY3NyZl9pZCIlNmEwODc0ZmUyM2I0OTlmMDI4ODdjZDYxZWU4NWEy
NzQiCmZsYXNoSUM6J0FjdGlvbkNvbnRyb2xsZXI6OkZsYXNoOjpGbGFzaEhh
c2h7AAY6CkB1c2VkewA=–d44a044b8b3a01495e7459675bdd89ff41d62303
Parameters: {“question_id”=>“8113”, “_method”=>“delete”,
“action”=>“destroy”, “survey_id”=>“344”, “id”=>“35843”,
“controller”=>“admin/choices”}
Rendering admin/choices/destroy
Completed in 0.01925 (51 reqs/sec) | Rendering: 0.01007 (52%) | DB:
0.00092 (4%) | 200 OK
[http://localhost/admin/surveys/344/questions/8113/choices/35843]

Processing ApplicationController#index (for 127.0.0.1 at 2008-08-06
21:53:31) [POST]
Session ID:
BAh7BzoMY3NyZl9pZCIlNmEwODc0ZmUyM2I0OTlmMDI4ODdjZDYxZWU4NWEy
NzQiCmZsYXNoSUM6J0FjdGlvbkNvbnRyb2xsZXI6OkZsYXNoOjpGbGFzaEhh
c2h7AAY6CkB1c2VkewA=–d44a044b8b3a01495e7459675bdd89ff41d62303
Parameters: {}

ActionController::MethodNotAllowed (Only get, put, and delete requests
are allowed.):
/usr/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/action_controller/routing/recognition_optimisation.rb:65:in
recognize_path' /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/action_controller/routing/route_set.rb:384:inrecognize’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/action_controller/dispatcher.rb:148:in
handle_request' /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/action_controller/dispatcher.rb:107:indispatch’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/action_controller/dispatcher.rb:104:in
synchronize' /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/action_controller/dispatcher.rb:104:indispatch’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/action_controller/dispatcher.rb:120:in
dispatch_cgi' /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/action_controller/dispatcher.rb:35:indispatch’
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/bin/…/lib/mongrel/rails.rb:76:in
process' /usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel/rails.rb:74:insynchronize’
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/bin/…/lib/mongrel/rails.rb:74:in
process' /usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:159:inprocess_client’
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:in
each' /usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:inprocess_client’
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in
run' /usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:ininitialize’
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in
new' /usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:inrun’
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in
initialize' /usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:innew’
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in
run' /usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel/configurator.rb:282:inrun’
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel/configurator.rb:281:in
each' /usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel/configurator.rb:281:inrun’
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/bin/mongrel_rails:128:in
run' /usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel/command.rb:212:inrun’
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/bin/mongrel_rails:281
/usr/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:502:in
load' /usr/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:502:inload’
/usr/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:354:in
new_constants_in' /usr/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:502:inload’
/usr/lib/ruby/gems/1.8/gems/rails-2.1.0/lib/commands/servers/mongrel.rb:64
/usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:27:in
gem_original_require' /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:27:inrequire’
/usr/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:509:in
require' /usr/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:354:innew_constants_in’
/usr/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:509:in
require' /usr/lib/ruby/gems/1.8/gems/rails-2.1.0/lib/commands/server.rb:39 /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:27:ingem_original_require’
/usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:27:in
`require’
./script/server:3

Rendering
/usr/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/action_controller/templates/rescues/layout.erb
(method_not_allowed)

On Aug 6, 7:58 am, David L. [email protected]
wrote:

Processing ChoicesController#destroy (for 127.0.0.1 at 2008-08-06
0.00092 (4%) | 200 OK
ActionController::MethodNotAllowed (Only get, put, and delete requests
synchronize' /usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel/rails.rb:74:ininitialize’
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel/configurator.rb:282:i­n
/usr/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependen­cies.rb:502:in
/usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:27:in
/usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:27:in
`require’
./script/server:3

Rendering
/usr/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/action_controller/template­s/rescues/layout.erb
(method_not_allowed)


Posted viahttp://www.ruby-forum.com/.

Same problem… running on windows XP 64

David

The method used is by default POST. You can also specify GET or you can
simulate PUT or DELETE over POST. All specified with options[:method]

link_to_remote “Destroy”, :url => edit_person_url(@person), :method =>
:get

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs