Sinatra PUT method not working?

For some reason, my "PUT" method is not caught by Sinatra using this html. Can someone help me spot the error? When I use the post action in my controller, it works as expected ...

<form method="post" action="/proposals/<% =@proposal.id %>/addItem"> <input type="hidden" name="_method" value="put"/> <div> <label for="item_id">Item list</label> <select title="Item ID" id="item_id" name='item_id'> <%@items.each do |item|%> <option value="<%=item.id%>"><%=item.name%></option> <%end%> </select> <input type="submit" value="Add"/></div> <label for="new_item_name">Create new item</label> <input type="text" id="new_item_name" name="new_item_name" /> <input type="submit" value="Create"/> </form> 
+4
source share
3 answers

Everything looks right. It looks like you either entered the route string incorrectly, or got on a different route to your put method. I was wondering, so I wrote a quick Sinatra application that used the put method, and it really works that way.

 #!/usr/bin/env ruby require 'rubygems' require 'sinatra' get '/' do <<-eos <html> <body> <form action="/putsomething" method="post"> <input type="hidden" name="_method" value="put" /> <input type="submit"> </form> </body> </html> eos end put '/putsomething' do "You put something!" end 
+9
source

Be sure to include Rack::MethodOverride in your config.ru:

 use Rack::MethodOverride 
+13
source

I just ran into this and none of the above tips helped. What I found:

The definition of the form should begin first with the action = and the second with the = method

correct form:

 <form action="/putsomething" method="POST"> <input type="hidden" name="_method" value="PUT" /> ... </form> 

irregular shape:

 <form method="POST" action="/putsomething"> <input type="hidden" name="_method" value="PUT" /> ... </form> 

The first worked for me, the second - no. Perhaps this helps.

0
source

All Articles