【问题标题】:Rails jQuery sortable unable to saveRails jQuery可排序无法保存
【发布时间】:2014-06-16 15:33:38
【问题描述】:

我正在尝试使用 jQuery 使我的表可排序(拖放),我遇到的问题是保存新的排序表,然后将其发布给其他用户或刷新浏览器时。

我正在使用:

gem 'acts_as_list' 导轨 3.2.17 红宝石 1.9.3p484

有没有更好的方法来做到这一点?

我的控制器上的排序和显示方法

默认排序 @episodes = current_user.episodes_for(@show).each 做 |episode| episode.position = params['episode'].index(episode.id.to_s) + 1 剧集.save 结尾 渲染:无=>真 结束

  def show
    @episodes = current_user.episodes_for(@show)
    @episodes.order('episode.position ASC')
    @episode = @episodes.where(id: params[:id]).first
  end

我的 JavaScript

<script>
$(window).load(function() {
    $("#sortthis").sortable({
      axis: 'y',
      placeholder: 'ui-state-highlight',
      cursor: 'move',
      dropOnempty: false,
      scroll: true,
      opacity: 0.4,

      update: function(event, ui){
        var itm_arr = $("#sortthis").sortable('toArray');
        var pobj = {episodes: itm_arr};
        $.post("/episodes/sort", pobj);
      }

    });
});
 </script>

我想在视图中排序的表格:

<tbody id='eps'>
<tr>
  <th>Title</th>
  <th>#</th>
  <th>Season</th>
  <th>Download?</th>
  <th>Shared?</th>
  <th>Length</th>
  <th>Status</th>
        <th></th>
</tr>
<% for episode in @episodes %>
  <tr>
    <td id="eps_<%= episode.id %>", width="20%"><%=h episode.title %></td>
    <td id="eps_<%= episode.id %>"><%=h episode.number %></td>
    <td id="eps_<%= episode.id %>"><%=h episode.season %></td>
    <td id="eps_<%= episode.id %>"><%=h episode.is_downloadable %></td>
    <td id="eps_<%= episode.id %>"><%=h episode.shared_with_dev %></td>
    <td id="eps_<%= episode.id %>"><%=h episode.length %></td>
    <td>
      <% if episode.video %>
        <%= link_to 'Replace Video', new_show_episode_video_path(episode.show, episode) %>
      <% else %>
        <%= link_to 'Upload Video', new_show_episode_video_path(episode.show, episode) %>
      <% end %>
    </td>
        <td>
        <%= link_to "View", [episode.show, episode] %> &nbsp;
    <%= link_to "Edit", edit_show_episode_path(episode.show, episode) if permitted_to? :update, episode %> &nbsp;
    <%= link_to "Delete", show_episode_path(episode.show, episode), :confirm => 'Are you sure?', :method => :delete if permitted_to? :delete, episode %>
    </td>
  </tr>
<% end %>

任何帮助将不胜感激!

【问题讨论】:

    标签: javascript ruby-on-rails jquery-ui acts-as-list


    【解决方案1】:

    我想通了,做了以下修改:

    观看次数:

    <tr id="<%= episode.id %>">
    

    和新的控制器

    def sort
      Episode.all.each do |e|
        if position = params[:episodes].index(e.id.to_s)
          e.update_attribute(:position, position + 1) unless e.position == position + 1
        end
      end
      render :nothing => true, :status => 200
    end
    

    终于将这个添加到我的模型中:

      acts_as_list
      default_scope order('position')
    

    如果有人遇到类似问题,请告诉我。

    【讨论】:

      猜你喜欢
      • 2012-12-13
      • 2017-11-24
      • 2011-11-12
      • 2017-10-22
      • 1970-01-01
      • 1970-01-01
      • 2011-06-03
      • 1970-01-01
      • 2016-05-01
      相关资源
      最近更新 更多