【发布时间】:2010-11-23 02:31:52
【问题描述】:
我在我的 RoR 项目中创建了一个可排序列表,不幸的是它没有保存列表位置。页面刷新后,项目将返回其正常位置。我已经粘贴了下面的代码,或者你可以 git 它:git://github.com/mdgrech/23notes-.git
app/views/notes/index.html.erb
/////////////////////////////////////////////////////////////////////////////////////////////
<div id="newNoteDiv"></div>
<ul id="notesList">
<% for note in @notes %>
<li id="<%=h note.position %>">
<span class="handle">[drag]</span>
<div id="listContent">
<h3><%= link_to note.title, edit_note_path(note) %></h3>
<p><%=h note.content %></p>
<%= link_to "Destroy", note, :confirm => 'Are you sure?', :method => :delete %>
</div>
</li>
<% end %>
</ul>
<%= sortable_element("notesList", :url => sort_notes_path, :handle => "handle" ) %>
app/controllers/notes_controller.rb
//////////////////////////////////////////////////////////////////////////////////////////
def index
@notes = Note.all(:order => "position")
end
def sort
params[:notes].each_with_index do |id, index|
Note.update_all(['position=?', index+1], ['id=?', id])
end
render :nothing => true
end
config/routes.rb
//////////////////////////////////////////////////////////////////////////////////////////
map.resources :notes, :collection => { :sort => :post }
map.root :notes
app/models/note.rb
//////////////////////////////////////////////////////////////////////////////////////////
class Note < ActiveRecord::Base
acts_as_list
end
【问题讨论】:
-
当您移动列表中的项目时,您在日志中看到了什么?
标签: ruby-on-rails jquery-ui-sortable