【发布时间】:2015-07-08 18:16:34
【问题描述】:
我已经学习 Sinatra 几天了,所以我还是个新手。我只是创建了一个笔记应用程序,并在应用程序中添加了一个删除功能。
这是我的控制器:
# Will display title of all notes
get '/' do
@notes = Note.all
erb :index
end
# Will display content within selected note
get '/:id' do
@note = Note.where(id: params[:id]).first
erb :note_description
end
# Will delete item from notes.
delete '/:id' do
Note.delete(params[:id])
redirect '/'
end
这是我对 html/erb 的看法:
<div class="container">
<p><a href="/">Return to Notes</a></p>
<h1><%= @note.title %></h1>
<p><%= @note.content %></p>
<form onsubmit="confirm('Are you sure you want to delete the following note?')" action="/<%= @note.id %>" method="POST">
<input type="hidden" name="_method" value="DELETE">
<button type="submit">Delete</button>
</form>
</div>
如您所见,我创建了一个delete 按钮,当您单击它时会弹出一个提交窗口。这将简单地验证用户是否真的想要删除该对象。但是会发生什么,无论我单击什么,该项目都会被删除,我将被重定向到主页。我想添加逻辑,如果用户单击确定,应用程序只会删除并将我重定向到主页。如果他们单击取消,则不会发生任何事情。任何建议或帮助将不胜感激。
【问题讨论】:
标签: html ruby forms controller sinatra