0

我正在尝试基于此示例实现可排序列表功能 - http://railscasts.com/episodes/147-sortable-lists-revised

问题是,当我放下我的元素时,我不断收到以下错误消息:

Started POST "/applications/sort" for 127.0.0.1 at 2012-12-04 11:14:45 -0600
Processing by ApplicationsController#sort as */*
  Parameters: {"application_application_field_attributes_0"=>["id"], "application_application_field_attributes_1"=>["id"], "application_application_field_attributes_2"=>["id"]}
Completed 500 Internal Server Error in 1ms

NoMethodError (undefined method `each_with_index' for nil:NilClass):
  app/controllers/applications_controller.rb:91:in `sort'

视图/应用程序/_form.html.erb

<%= form_for(@application, :class => "main") do |f| %>
...
<div class="sortable" data-update-url="<%= sort_applications_url %>">
    <%= f.fields_for :application_field do |fields| %>
        <%= render 'application_field_fields', f: fields %>  
    <% end %>
</div>
<% end %>

视图/应用程序/_application_field_fields.html.erb

<div class="formRow" id="<%= f.object.position %>">
    <div class="grid2"><%= f.label :field_name %></div>
    <div class="grid3"><%= f.text_field :field_name %></div>
    <div class="grid2"><%= f.label :field_type %></div>
    <div class="grid3"><%= f.text_field :field_type %></div>
    <!-- Need to store the hidden field _destroy here so we can access it via jQuery -->
    <%= f.hidden_field :_destroy %>
    <div class="grid2" align="center"><%= link_to "Remove", '#', class: "remove_fields"%></div>
    <div class="clear"></div>
</div>

应用程序.js

$('.sortable').sortable({ 
    placeholder: "ui-state-highlight",
    forcePlaceholderSize: true,
    start: function(event, ui) {
        ui.item.addClass('dragging');
    },
    stop: function(event, ui) {
        ui.item.removeClass('dragging');
    },
    update: function(event, ui) {
        $.post($(this).data('update-url'), $(this).sortable('serialize'));
    }
});

控制器/应用程序#sort

# POST /applications/sort
  def sort
    puts '*********'
    puts '#####'
    puts 'params = ' #{params}
    puts '********'
    params[:application].each_with_index do |id, index| 
        Application.update_all({position: index+1}, {id: id})
    end
    render :nothing => true
  end

当我删除一个可排序的元素时,你可以看到我的参数哈希是空的。为什么会这样,我该如何解决?

编辑

我将发布我的模型,以便您查看关系

class Application < ActiveRecord::Base
    belongs_to :company
    has_many :application_field, :order => "position"
    accepts_nested_attributes_for :application_field, :allow_destroy => true
    attr_accessible :application_name, :application_field_attributes
end
class ApplicationField < ActiveRecord::Base
    belongs_to :application
    has_many :application_fields_value
    attr_accessible :field_name, :field_type, :field_value, :application_field_values_attributes
    accepts_nested_attributes_for :application_fields_value, :allow_destroy => true
end
4

3 回答 3

0

你的参数没有正确的结构,所以params[:application]是 nil。

于 2012-12-04T17:33:36.533 回答
0

我通过改变解决了这个问题

<div class="formRow" id="<%= f.object.position %>">
    <div class="grid2"><%= f.label :field_name %></div>
    <div class="grid3"><%= f.text_field :field_name %></div>
    <div class="grid2"><%= f.label :field_type %></div>
    <div class="grid3"><%= f.text_field :field_type %></div>
    <!-- Need to store the hidden field _destroy here so we can access it via jQuery -->
    <%= f.hidden_field :_destroy %>
    <div class="grid2" align="center"><%= link_to "Remove", '#', class: "remove_fields"%></div>
    <div class="clear"></div>
</div>

<%= content_tag_for :div, f.object, :class => "formRow" do %>
    <div class="grid2"><%= f.label :field_name %></div>
    <div class="grid3"><%= f.text_field :field_name %></div>
    <div class="grid2"><%= f.label :field_type %></div>
    <div class="grid3"><%= f.text_field :field_type %></div>
    <!-- Need to store the hidden field _destroy here so we can access it via jQuery -->
    <%= f.hidden_field :_destroy %>
    <div class="grid2" align="center"><%= link_to "Remove", '#', class: "remove_fields"%></div>
    <div class="clear"></div>
<% end %>

有人可以解释为什么这有效吗?

于 2012-12-05T03:28:01.923 回答
0

这是因为您没有将值传递给表单上的应用程序。然后,在您看来,它尝试迭代一个集合,但它是一个 nil 值。

尝试通过这样做来调试它

puts "params => #{params.inspect}" 

在您的排序操作中的迭代之前。

如果你不能通过这样做解决你的问题,请将它的结果粘贴到这里。

于 2012-12-04T17:40:50.837 回答