Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sinatra PUT method not working?

Tags:

ruby

sinatra

For some reason, my "PUT" method isn't caught by Sinatra using this html. Can someone help me spot the mistake? When I use a "post" action in my controller, it works just the way it is expected...

<form method="post" action="/proposals/<%[email protected]%>/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>
like image 809
Olivier Tremblay Avatar asked Nov 29 '22 04:11

Olivier Tremblay


2 Answers

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

use Rack::MethodOverride
like image 76
awilkening Avatar answered Jan 22 '23 12:01

awilkening


That all looks correct. It looks like you either wrote the route string wrong, or it's being caught by another route before your put method. I was curious about this so I wrote up a quick Sinatra app that used a put method, and it does indeed work this 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
like image 20
AboutRuby Avatar answered Jan 22 '23 11:01

AboutRuby