为什么我得到“未定义的方法`map'为nil:NilClass”错误

时间:2016-02-04 21:24:06

标签: jquery ruby-on-rails form-for

我正在创建一个收集项目信息的表单。型号,品牌,类别等。

我正在使用闭包树gem存储嵌套类别和jquery ajax来填充嵌套类别选项。

当我填写表格并提交时。然后我收到了错误。我无法弄清楚为什么我会收到这个错误。我觉得一切都好。

Started POST "/items" for 127.0.0.1 at 2016-02-04 23:32:43 +0200
Processing by ItemsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"TQoetryLPx2w7kIHki6mB4UDJFwjXm1nOvwPM120j2eP5wBiVVb22h1AjD487unP1MSZtnqddOe3xdTmjbBkwA==", "item"=>{"name"=>"name", "description"=>"desc", "brand"=>"brand", "model"=>"model", "quantity"=>"1", "unit_id"=>"2"}, "category"=>{"id1"=>"30", "id2"=>"31"}, "commit"=>"Create Item"}
  User Load (0.1ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 1  ORDER BY `users`.`id` ASC LIMIT 1
asdas   (0.1ms)  BEGIN
   (0.1ms)  ROLLBACK
  Rendered items/_form.html.erb (7.0ms)
  Rendered items/new.html.erb within layouts/application (8.1ms)
Completed 500 Internal Server Error in 17ms (ActiveRecord: 0.3ms)

Rendered items/_form.html.erb (7.0ms)
Rendered items/new.html.erb within layouts/application (8.1ms)
Completed 500 Internal Server Error in 17ms (ActiveRecord: 0.3ms)
undefined method `map' for nil:NilClass

ActionView::Template::Error (undefined method `map' for nil:NilClass):              
    <div class="input-group">
    <span class="input-group-addon">1</span>
-->> <%= f.collection_select(:category_id, @categories, :id, :name, {}, { :id => 'C1', name: 'category[id1]', class: 'form-control'}) %>
    </div>

  app/views/items/_form.html.erb:53:in `block in _app_views_items__form_html_erb__3272575439911272512_70018975348520'
  app/views/items/_form.html.erb:1:in `_app_views_items__form_html_erb__3272575439911272512_70018975348520'
  app/views/items/new.html.erb:5:in `_app_views_items_new_html_erb__1044924137833435696_47249300'
  app/controllers/items_controller.rb:34:in `create'

_form.html.erb

<%= form_for @item, :html => { :class => "form-horizontal item" } do |f| %>

  <% if @item.errors.any? %>
    <div id="error_expl" class="panel panel-danger">
      <div class="panel-heading">
        <h3 class="panel-title"><%= pluralize(@item.errors.count, "error") %> prohibited this item from being saved:</h3>
      </div>
      <div class="panel-body">
        <ul>
        <% @item.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
        </ul>
      </div>
    </div>
  <% end %>



  <div class="form-group">
    <%= f.label :name, :class => 'control-label col-lg-2' %>
    <div class="col-lg-8">
      <%= f.text_field :name, :class => 'form-control' %>
    </div>
    <%=f.error_span(:name) %>
  </div>
  <div class="form-group">
    <%= f.label :description, :class => 'control-label col-lg-2' %>
    <div class="col-lg-8">
      <%= f.text_field :description, :class => 'form-control' %>
    </div>
    <%=f.error_span(:description) %>
  </div>
  <div class="form-group">
    <%= f.label :category_id, :class => 'control-label col-lg-2' %>
    <div class="col-lg-8">
    <div class="input-group">
    <span class="input-group-addon">1</span>
      <%= f.collection_select(:category_id, @categories, :id, :name, {}, { :id => 'C1', name: 'category[id1]', class: 'form-control'}) %>
    </div>

    <div class="input-group">
    <span class="input-group-addon">2</span>
    <%= f.collection_select(:category_id, @categories, :id, :name, {}, { :id => 'C2', name: 'category[id2]', class: 'form-control'}) %>

    </div>
    <div class="input-group">
    <span class="input-group-addon">3</span>
    <%= f.collection_select(:category_id, @categories, :id, :name, {}, { :id => 'C3', name: 'category[id3]', class: 'form-control'}) %>

    </div>
    <div class="input-group">
    <span class="input-group-addon">4</span>
    <%= f.collection_select(:category_id, @categories, :id, :name, {}, { :id => 'C4', name: 'category[id4]', class: 'form-control'}) %>

    </div>
    <div class="input-group">
    <span class="input-group-addon">5</span>
    <%= f.collection_select(:category_id, @categories, :id, :name, {}, { :id => 'C5', name: 'category[id5]', class: 'form-control'}) %>
    </div>
    </div>
    <%=f.error_span(:category_id) %>
  </div>
  <div class="form-group">
    <%= f.label :brand, :class => 'control-label col-lg-2' %>
    <div class="col-lg-8">
      <%= f.text_field :brand, :class => 'form-control' %>
    </div>
    <%=f.error_span(:brand) %>
  </div>
  <div class="form-group">
    <%= f.label :model, :class => 'control-label col-lg-2' %>
    <div class="col-lg-8">
      <%= f.text_field :model, :class => 'form-control' %>
    </div>
    <%=f.error_span(:model) %>
  </div>
  <div class="form-group">
    <%= f.label :unit_id, :class => 'control-label col-lg-2' %>
    <div class="col-lg-8">
        <div class = "input-group">
            <%= f.number_field :quantity, :class => 'form-control col-lg-2', :html => {'aria-label' => '...'} %>
                <%= f.collection_select(:unit_id, @units, :id, :name, {}, { class: 'form-control col-lg-1'}) %>
        </div>
    </div>
    <%=f.error_span(:unit) %>
  </div>

  <div class="form-group">
    <div class="col-lg-offset-2 col-lg-8">
      <%= f.submit nil, :class => 'btn btn-primary' %>
      <%= link_to t('.cancel', :default => t("helpers.links.cancel")),
                items_path, :class => 'btn btn-default' %>
    </div>
  </div>

<% end %>
<script>
$(document).ready( function() {
    $('#C1').click(function() {
      $.ajax({
        url: "<%= children_category_path %>",
    data: {
        id : $('#C1 option:selected').val(),
        which : "C1",
        form : "item",
        action: "<%= params[:action] %>"
    },
        dataType: "script"
    });
    });
    $('#C2').click(function() {
    $.ajax({
        url: "<%= children_category_path %>",
        data: {
        id : $('#C2 option:selected').val(),
        which : "C2",
        form : "item",
        action: "<%= params[:action] %>"
        },
        dataType: "script"
      });
    });
    $('#C3').click(function() {
      $.ajax({
        url: "<%= children_category_path %>",
        data: {
        id : $('#C3 option:selected').val(),
        which : "C3",
        form : "item",
        action: "<%= params[:action] %>"
        },
        dataType: "script"
      });
    });
    $('#C4').click(function() {
      $.ajax({
        url: "<%= children_category_path %>",
        data: {
            id : $('#C4 option:selected').val(),
        which : "C4",
        form : "item",
        action: "<%= params[:action] %>"
      },
        dataType: "script"
      });
    });
});
</script>

items_controller.rb

def new

        @item = Item.new

        @categories = Category.roots

        @units = Unit.all

    end

    def create


        @item = Item.new(create_params)

        if @item.save

            redirect_to @item

        else

            render 'new'

        end

    end

  private

  def create_params

        data = params.require(:item).permit(:name, :description, :brand, :model, :quantity, :unit_id)

        category_id = params.require(:category).permit(:id1,:id2,:id3, :id4, :id5).values.reject(&:empty?).compact.last

        return data.merge(Hash["category_id" => category_id ])

    end

类别根返回根类别,在此示例中,我有一个根类别。

Category.roots
<Category id: 30, name: "Category", parent_id: nil, sort_order: nil, created_at: "2016-02-04 20:43:57", updated_at: "2016-02-04 20:43:57", custom_fields: {}

1 个答案:

答案 0 :(得分:0)

您需要添加@categories = Category.roots@units = Unit.all 控制器中的create方法。