2017-06-28 62 views
-1

未定義方法`map'for nil: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) 
+0

你可以共享控制檯日誌以及產品控制器代碼 – Aashish

+0

@Aashish 下面是產品控制器創建產品代碼: \t高清新 \t \t '@' 產品= Product.new \t \t '@' 類別= {Category.all.map | C | [c.name,c.id]} \t端 \t DEF創建 \t \t '@' 的產品= Product.new(product_params) \t \t'@'product.category_id =參數[:CATEGORY_ID] \t \t如果'@'product.save \t \t \t redirect_to的root_path \t \t其他 \t \t \t呈現 '新' \t \t結束 \t結束 和控制檯給出了同樣的錯誤: 無法呈現控制檯從89.218.94.140!允許的網絡:127.0.0.1,:: 1,127.0.0.0/127.255.255.255 –

+0

請在問題本身中添加日誌。因此,任何閱讀您的問題的人都不需要閱讀評論部分以查找此日誌。 – RatDon

回答

0

undefined method `map' for nil:NilClass

你可能會得到在您看來這個錯誤,上線:

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

這意味着@categories變量是undefined在您的控制器操作提供此視圖最有可能是ProductsController#new。確保您在該動作中爲@categories變量賦值,例如

@categories = Category.all 

UPDATE:

正如您的評論中提到,如果您在控制器賦值的變量@categories

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

然後確保表Category具有價值。看起來好像Category.all正在返回nil這是導致此錯誤。您可以通過在您的終端上啓動rails console並在其上運行Category.all命令輕鬆進行測試。

+0

我在這裏創造新的產品和defiine類高清新 \t \t 「@」 產品= Product.new \t \t 「@」 類別= {Category.all.map | C | [c.name,c.id]} \t end –

+0

@AlinaDyussupova哪裏?你能用'ProductsController'代碼更新你的問題,並且還可以共享完整的錯誤堆棧跟蹤。 –

+0

@AlinaDyussupova我已根據您的評論爲我的答案添加了更新部分。 –

1

undefined method `map' for nil:NilClass

@categories未定義。請確保您在相關controller#action

中定義了@categories另外,您寫錯了。options_for_select預計集裝箱(無論是散/陣列/枚舉)

Accepts a container (hash, array, enumerable, your type) and returns a string of option tags. Given a container where the elements respond to first and last (such as a two-element array), the 「lasts」 serve as option values and the 「firsts」 as option text. Hashes are turned into this form automatically, so the keys become 「firsts」 and values become lasts. If selected is specified, the matching 「last」 or element will get the selected option-tag.

你需要將其更改爲類似

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

看來你正在使用的simple_form,所以對顯示select標籤你有以下選項

選項#1

f.input :category_id, collection: @categories, prompt: 'Category' 

確保@categories包含數組元素

選項#2

使用simple_formAssociations如下(請訪問鏈接查看更多細節)

f.association :category, prompt: 'Category' 

希望這將幫助您解決你的問題

+0

嘗試了這兩個選項,它沒有給我錯誤,但它將我重定向到/ products頁面,而不是頁面的產品有相同形式的視圖:/無論如何,感謝您的幫助,認爲代碼中存在更深層次的問題 –