开发者

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.s开发者_Python百科ave
    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>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜