在rails中创建新产品时出错

时间:2017-06-28 04:10:48

标签: ruby-on-rails dictionary controller categories

未定义的方法`map'为零:NilClass 你的意思是?挖掘

产品控制器

 <%= f.input  :title , label: "Name of the product: " %>
  <%= f.input  :description, label: "Description of the product: " %>
  <%= select_tag(:category_id, options_for_select(@categories), :promt => "Category")%>
  <%= f.input  :price, as: :integer , label: "Price of the product: " %>
  <%= f.input  :discount, label: "Discount for the product" %>
  <%= f.input  :image, as: :file,label: "Please upload image of the product" %>

此处出现在控制台中的错误

Cannot render console from 89.218.94.140! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by ProductsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"PSg52JmMvZq3dLjje4cEYuYe4ySr3v0DMx5pLv723KKYnQ0awd+ypWii+foe4c6HV6ddq1xEuyFMUwAgBNS0sA==", "product"=>{"title"=>"For her", "description"=>"Gentle and beauty", "price"=>"8000", "discount"=>"3300", "image"=>#<ActionDispatch::Http::UploadedFile:0x007fd3f0cd2140 @tempfile=#<Tempfile:/tmp/RackMultipart20170628-4090-3uc3bp.jpg>, @original_filename="705c9f06e0f2a2b960d54813b871e7eb.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"product[image]\"; filename=\"705c9f06e0f2a2b960d54813b871e7eb.jpg\"\r\nContent-Type: image/jpeg\r\n">, "available"=>"1"}, "category_id"=>"1", "commit"=>"Create Product"}
Command :: PATH=/usr/local/bin/:$PATH; file -b --mime '/tmp/65a151e5e2c95cb454ff8af39951145320170628-4090-rlpn75.jpg'
Command :: PATH=/usr/local/bin/:$PATH; identify -format '%wx%h,%[exif:orientation]' '/tmp/65a151e5e2c95cb454ff8af39951145320170628-4090-9s8vu4.jpg[0]' 2>/dev/null
Command :: PATH=/usr/local/bin/:$PATH; identify -format %m '/tmp/65a151e5e2c95cb454ff8af39951145320170628-4090-9s8vu4.jpg[0]'
Command :: PATH=/usr/local/bin/:$PATH; convert '/tmp/65a151e5e2c95cb454ff8af39951145320170628-4090-9s8vu4.jpg[0]' -auto-orient -resize "600x600>" '/tmp/a5967e19d796bb8d7db2a350531f598020170628-4090-7kuj4w'
Command :: PATH=/usr/local/bin/:$PATH; identify -format '%wx%h,%[exif:orientation]' '/tmp/65a151e5e2c95cb454ff8af39951145320170628-4090-9s8vu4.jpg[0]' 2>/dev/null
Command :: PATH=/usr/local/bin/:$PATH; identify -format %m '/tmp/65a151e5e2c95cb454ff8af39951145320170628-4090-9s8vu4.jpg[0]'
Command :: PATH=/usr/local/bin/:$PATH; convert '/tmp/65a151e5e2c95cb454ff8af39951145320170628-4090-9s8vu4.jpg[0]' -auto-orient -resize "300x300>" '/tmp/a5967e19d796bb8d7db2a350531f598020170628-4090-zf5vc5'
Command :: PATH=/usr/local/bin/:$PATH; identify -format '%wx%h,%[exif:orientation]' '/tmp/65a151e5e2c95cb454ff8af39951145320170628-4090-9s8vu4.jpg[0]' 2>/dev/null
Command :: PATH=/usr/local/bin/:$PATH; identify -format %m '/tmp/65a151e5e2c95cb454ff8af39951145320170628-4090-9s8vu4.jpg[0]'
Command :: PATH=/usr/local/bin/:$PATH; convert '/tmp/65a151e5e2c95cb454ff8af39951145320170628-4090-9s8vu4.jpg[0]' -auto-orient -resize "150x" -crop "150x0+0+102" +repage '/tmp/a5967e19d796bb8d7db2a350531f598020170628-4090-1c398r8'
   (0.1ms)  begin transaction
  Category Load (0.3ms)  SELECT  "categories".* FROM "categories" WHERE "categories"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
Command :: PATH=/usr/local/bin/:$PATH; file -b --mime '/tmp/65a151e5e2c95cb454ff8af39951145320170628-4090-fyglg3.jpg'
   (0.1ms)  rollback transaction
  Rendering products/new.html.erb within layouts/application
  Rendered products/_form.html.erb (5.9ms)
  Rendered products/new.html.erb within layouts/application (7.0ms)
Completed 500 Internal Server Error in 346ms (ActiveRecord: 0.4ms)

3 个答案:

答案 0 :(得分:1)

  

未定义的方法`map&#39;为零:NilClass

@categories未定义。确保您已在相关的@categories

中定义了controller#action

另外,你写错了。 options_for_select期望容器(散列/数组/可枚举)

  

接受一个容器(hash,array,enumerable,你的类型)并返回一个   选项标签字符串。给定元素响应的容器   第一个和最后一个(例如一个双元素数组),“持续”作为   选项值和“第一”作为选项文本。哈希变成了   这种形式自动生成,因此键变为“第一”,价值变为   持续。如果指定了selected,匹配的“last”或元素将   获取所选的选项标签。

您需要将其更改为

<%= select_tag(:category_id, options_for_select(@categories.collect{ |u| [u.name, u.id] }), :prompt => "Category")%>

答案 1 :(得分:1)

您好像正在使用select,因此对于显示f.input :category_id, collection: @categories, prompt: 'Category' 标记,您有以下选项

选项#1

@categories

确保simple_form包含数组元素

选项#2

使用 f.association :category, prompt: 'Category' Associations如下(访问链接了解详情)

build.gradle (Module: app)

希望这可以帮助您解决问题

答案 2 :(得分:0)

  

未定义的方法`map&#39;为零:NilClass

您可能在视图中收到此错误,在线:

<%= select_tag(:category_id, options_for_select(@categories), :promt => "Category")%>

这意味着您的控制器操作中的@categories变量为undefined,为此视图提供了ProductsController#new。确保您在该操作中为@categories变量赋值,例如

@categories = Category.all

更新:

如评论中所述,如果您已在控制器中分配变量@categories,如

def new 
  @product = Product.new 
  @categories = Category.all.map{|c| [c.name, c.id]} 
end

然后确保表Category具有值。似乎Category.all正在返回nil,导致此错误。您可以通过在终端上启动rails console并在其上运行Category.all命令来轻松测试。