2014-10-03 46 views
0

使用Rails 4和Ruby 1.9.3。造型是由基于引导3的内部开发的宝石处理。如何在轨道上对物品进行排序

我一直在寻找这个答案,并找到了一些不同的例子,显示如何做到这一点与视图中的基本列表。 railscast就是我看过的一个例子。

http://railscasts.com/episodes/147-sortable-lists

不过,我想使用的部分,但没有成功嵌套形式的轨道来实现这一目标。可悲的是,我对ruby,rails并不熟悉,并且对JavaScript没有任何先验知识,所以这只是一个学习曲线。

我的服务模式涉及通过服务场所的地方模式。 service_places.position字段包含停靠点的顺序。

我的嵌套形式(_form.html.erb)的服务如下:下面

<!-- Adds the Service_Places (stops) associations via partial (sort applied in model) --> 
<div> 
    <div class="links" id="sortable"> 
    <%= link_to_add_association 'Add Stop', f, :service_places, :class => "btn btn-default", :data => {"association-insertion-method" => "after" } %> 
    </div> 
    <%= f.fields_for :service_places do |service_places| %> 
    <%= render 'service_place_fields', :f => service_places %> 
    <% end %> 
</div> 

我service_place部分所示:

<!-- This will hold the partial form for service places --> 
<div class="nested-fields"> 
    <%= f.label :service_place, "Stops", :class=>"col-sm-2 control-label" %> 
    <div class="col-sm-1"> 
    <%= f.text_field :position, :class=> "form-control", :placeholder => "Position" %> 
    </div> 
    <div class="col-sm-3"> 
    <%= f.collection_select :place_id, Place.where('active = true').order(:place_name), :id, :place_name, :prompt => "Select Place" %> 
    </div> 
    <div> 
    <%= link_to_remove_association "Remove Stop", f, :class => "btn btn-default" %> 
    </div> 
</div> 

我开始寻找在试图分配ID到每个service_place部分DIV标签,但无法使其工作。

我想知道的问题是:

1)是否有可能允许用户重新排序表中的项目?并将新订单保存在服务器中? 2)如果有可能有人给我一个关于如何去做这件事的暗示。

在此先感谢您花时间看这篇文章。

+0

试试' <%= f.fields_for:service_places,f.object.service_places.order('position asc')do | service_places | %>' – juanpastas 2014-10-03 16:36:46

+0

感谢您的评论胡安。那不是我所追求的。我正在寻找的是用户能够重新排序表单中的数据,并基于此更新适当的位置字段(可能在提交时)。答案将以某种格式涉及JavaScript。 railscast是我试图实现的一个很好的例子。 – 2014-10-06 12:26:15

+0

我现在明白了,请编辑您的问题,使其更清晰。 – juanpastas 2014-10-06 16:53:10

回答

0

我的同事向我展示了一种使用“作为列表”gem来达到此目的的方法。
他的博客可以在这里找到:

http://nicholshayes.co.uk/blog/?p=344

得到这个工作我做了修改,如下图所示。

1)在我的服务模式,我添加这三种方法:

# Used in the positioning of service places using the acts_as_list gem 
    def method_missing(symbol, *args, &block) 
    if acts_as_list_method?(symbol) 
     pass_method_to_service_service_place(symbol, args.first) 
    else 
     super 
    end 
    end 

    # Used in the positioning of service places using the acts_as_list gem 
    def pass_method_to_service_service_place(symbol, service_place) 
    raise "A Service_Place is needed for re-ordering places" unless service_place.kind_of? ServicePlace 
    service_place.send(symbol) if service_place 
    end 

    # Used in the positioning of service places using the acts_as_list gem 
    def acts_as_list_method?(symbol) 
    ActiveRecord::Acts::List::InstanceMethods.instance_methods.include?(symbol.to_sym) 
    end 

2)新增航线的

resources :services, :concerns => :paginatable, only: [:create, :destroy, :edit, :index, :new, :show, :update] do 

    # Required for the re-ordering of the routes 
    resources :service_places do 
     member do 
     get :move_up 
     get :move_down 
     end 
    end 

    end 

3)修改服务show.html

<% @service.service_places.reorder("service_places.position asc").each do |serviceplace| %> 

    <dd> 

    <% if user_signed_in? %> 
     <% if @service.first?(serviceplace) %> 
     <%= link_to('', move_up_service_service_place_path(serviceplace, service_id: @service), class: "btn btn-default btn-xs glyphicon glyphicon-arrow-up invisible") %> 
     <% else %> 
     <%= link_to('', move_up_service_service_place_path(serviceplace, service_id: @service), class: "btn btn-default btn-xs glyphicon glyphicon-arrow-up") %> 
     <% end %> 

     <% if @service.last?(serviceplace) %> 
     <%= link_to('', move_down_service_service_place_path(serviceplace, service_id: @service), class: "btn btn-default btn-xs glyphicon glyphicon-arrow-down invisible") %> 
     <% else %> 
     <%= link_to('', move_down_service_service_place_path(serviceplace, service_id: @service), class: "btn btn-default btn-xs glyphicon glyphicon-arrow-down") %> 
     <% end %> 
    <% end %> 

    <!-- Unfortunately this is not very efficient way of showing the place because it calls the db for each item. --> 
    <%= Place.find(serviceplace.place_id).place_name %> 

    </dd> 

<% end %>