我正在使用Acts As List宝石。我有一个表,我试图通过向上/向下拖动行然后将该位置保存到数据库来动态重新排序。
目前,如果我拖放一行然后刷新页面,它总是将该行放在顶部,而不管它被拖到哪里:
模型/ border.rb
acts_as_list :column => :position
控制器/ borders_controller.rb
def sort
@border = Border.find(params[:id])
@border.insert_at(params[:position].to_i+1)
render nothing: true
end
视图/ index.html.erb
<table class="table table-condensed table-bordered table-striped" id="sortable" data-update_url="<%= sort_admin_borders_path%>">
<tr>
<th style="width:10px !important;">Position</th>
<th>Name</th>
<th>Colour</th>
<th>Texture</th>
<th>Pattern</th>
<th></th>
</tr>
<% @borders.each do |border| %>
<tr data-item_id="<%=border.id%>" class='draggable-item' style="cursor: row-resize;">
<td><%= border.position %></td>
<td><%= link_to border.name, admin_border_path(border) %></td>
<td style="background: <%= border.colour %> "></td>
<td><%= border.texture %></td>
<td><%= border.pattern %></td>
<td><%= link_to "Delete", admin_border_path(border), method: :delete, :confirm => "Are you sure you want to delete this border?" %></td>
</tr>
<% end %>
<% if @borders.empty? %>
<tr class="warning"><td colspan="5"><i class="icon-exclamation-sign"></i> No borders found</td></tr>
<% end %>
</table>
我移动一行时的控制台:
**Started POST "/admin/borders/sort" for 127.0.0.1 at 2014-08-12 10:50:34 +0100
Processing by Admin::BordersController#sort as JSON
Parameters: {"id"=>"10", "display_order_position"=>"3"}
Admin Load (16.5ms) SELECT "admins".* FROM "admins" WHERE "admins"."id" = 23 LIMIT 1
Border Load (1.6ms) SELECT "components".* FROM "components" WHERE "components"."type" IN ('Border') AND "components"."id" = $1 LIMIT 1 [["id", "10"]]
SQL (7.8ms) UPDATE "components" SET position = (position + 1) WHERE "components"."type" IN ('Border') AND (1 = 1 AND position >= 1 AND position < 2)
(0.9ms) BEGIN
(1.6ms) UPDATE "components" SET "position" = 1, "updated_at" = '2014-08-12 09:50:34.400216' WHERE "components"."type" IN ('Border') AND "components"."id" = 10
(1.9ms) SELECT COUNT(*) FROM "components" WHERE "components"."type" IN ('Border') AND (1 = 1 AND position = 1)
(0.5ms) COMMIT
Rendered text template (0.0ms)**
答案 0 :(得分:0)
您的问题是表单正在发送params[:display_order_position]
(在您的日志中看到),并且您在控制器中使用params[:position]
。 params[:position]
将nil
,并且acts_as_list会在此上调用to_i
,nil.to_i
将显示为0,因此就像您尝试将位置设置为0一样每一次。
您需要更改javascript才能发送params[:position]
或更改控制器代码以使用params[:display_order_position]
。