Ruby-on-rails nil:NilClass 的未定义方法`map',是什么原因造成的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16960294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:07:50  来源:igfitidea点击:

undefined method `map' for nil:NilClass , what causes this?

ruby-on-rails

提问by Hussain Akhtar Wahid 'Ghouri'

Hi i am facing this error , completely new to rails so cant figure out what is causing it

嗨,我正面临这个错误,对 Rails 来说是全新的,所以无法弄清楚是什么原因造成的

my newBook.html.erb

我的 newBook.html.erb

<html>
    <head>
        <title> new Book </title>
    </head>
    <body>
        <h1><%= @hello_message %></h1>
        <h1>Add new book</h1>
        <%= form_tag :action => 'create' %>
        <p>
            <label for="book_title">Title</label>:
            <%= text_field 'book', 'title' %>
        </p>
        <p>
            <label for="book_price">Price</label>:
            <%= text_field 'book', 'price' %>
        </p>
        <p>
            <label for="book_subject">Subject</label>:
            <%= collection_select(:book,:subject_id,@subjects,:id,:name) %>
        </p>
        <p>
            <label for="book_description">Description</label>
            <br/>
            <%= text_area 'book', 'description' %>
        </p>
        <%= submit_tag "Create" %>
        <%= end_form_tag %>
        <%= link_to 'Back', {:action => 'list'} %>
    </body>
</html>

my book model : book.rb

我的书模型:book.rb

class Book < ActiveRecord::Base
  attr_accessible :title, :price,:description , :created_at 
  belongs_to :subject
  validates_presence_of :title
  validates_numericality_of :price, :message=>"Error Message"
end

my subject model : subject.rb

我的主题模型:subject.rb

class Subject < ActiveRecord::Base
  attr_accessible :name
  has_many :book

end

stack trace is :

堆栈跟踪是:

actionpack (3.2.13) lib/action_view/helpers/form_options_helper.rb:364:in `options_from_collection_for_select'
actionpack (3.2.13) lib/action_view/helpers/form_options_helper.rb:600:in `to_collection_select_tag'
actionpack (3.2.13) lib/action_view/helpers/form_options_helper.rb:191:in `collection_select'
app/views/home/newBook.html.erb:19:in `_app_views_home_new_ook_html_erb__299261930_24178164'
actionpack (3.2.13) lib/action_view/template.rb:145:in `block in render'
activesupport (3.2.13) lib/active_support/notifications.rb:125:in `instrument'
actionpack (3.2.13) lib/action_view/template.rb:143:in `render'
# -- snipped --

回答by Matt

<%= collection_select(:book,:subject_id,@subjects,:id,:name) %>

Your @subjectsobject is undefined. You need in your controller action for this page something that sets the contents of that variable, for example:

您的@subjects对象未定义。您需要在此页面的控制器操作中设置该变量的内容,例如:

@subjects = Subject.all 

See the source for options_from_collection_for_select- first thing it does is a map call on the collection passed to it (in your case @subjects).

请参阅options_from_collection_for_select的来源- 它所做的第一件事是对传递给它的集合进行映射调用(在您的情况下@subjects)。

回答by Deepika

<%= collection_select(:book,:subject_id,Subject.all,:id,:name) %>

回答by TattooedJoey

For people looking at this post and the answer isn't working for them;

对于看这篇文章的人来说,答案对他们不起作用;

I was trying to link the table "Locations" in the table "Departments".

我试图链接“部门”表中的“位置”表。

The following code worked for me, having taking inspiration from the original answer;

以下代码对我有用,从原始答案中获得灵感;

# In app/views/departments/_form.html.erb:
...
    <p>
        <%= f.label :location %><br>
        <%= f.collection_select(:location,@locations,:id,:address) %>
    </p>    
...

# In app/controllers/departments_controller.rb;

  def new
    @locations = Location.all
  ....
  end