crayfish
crayfish

Reputation: 147

Sinatra HTTP 'PUT' method

Something's wrong with the PUT action here, the form gets processed but the updated field is not being saved.

I've did what Sinatra users are doing, by adding in "_method" for Sinatra to recognise that's its a HTTP PUT action. Could anyone spot any mistake here?

# edit
get '/entries/*/:id/edit' do
  @entry = Entries.get(params[:id])
  @title = "edit"
  erb :edit, :layout => :edit_layout
end

# update
put '/entries/:id' do
  @entry = Entries.get(params[:id])
  if @entry.save
    redirect "/entries/id=#{@entry.id}"
  else
    redirect "/enewsletters"
  end
end

<!-- Edit form -->
<form action="/enewsletters/edit/<%= @entry.id %>" method="post">
  <input name="_method" value="put" type="hidden"/>
  <p>
    <label>Content</label><br/>
    <input type="text" name="entry[title]" value="<%= @enew.title %>">
  </p>
  <p>
    <input type="submit" name="commit" value="update">
  </p>
</form>

Upvotes: 1

Views: 3219

Answers (1)

theIV
theIV

Reputation: 25774

You don't seem to be doing any update to the @entry, you're just fetching the specific entry with the id from params. Are you using ActiveRecord? If so, instead of @entry.save, try @entry.update_attributes(params[:entry]).

Edit: I'm guessing you're not using AR since I just noticed the .get call. Whatever ORM you are using must have an easy way to update the attributes and then save the record.

Upvotes: 3

Related Questions