Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails nil:NilClass的未定义方法“map”,这是什么原因?_Ruby On Rails - Fatal编程技术网

Ruby on rails nil:NilClass的未定义方法“map”,这是什么原因?

Ruby on rails nil:NilClass的未定义方法“map”,这是什么原因?,ruby-on-rails,Ruby On Rails,嗨,我面临着这个错误, 对rails来说是全新的,所以无法找出原因 my newBook.html.erb <html> <head> <title> new Book </title> </head> <body> <h1><%= @hello_message %></h1> <h1>Add new bo

嗨,我面临着这个错误, 对rails来说是全新的,所以无法找出原因

my 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>

查看来源-它所做的第一件事是在您的case@subjects中对传递给它的集合进行映射调用。

对于查看此帖子的人,答案对他们不起作用

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

下面的代码为我工作,从原始答案中获得灵感

# 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

我最近遇到了这个问题。我已经为使用GET调用表单页面的新方法定义了对象,但在为新问题和创建问题定义对象后,忘记为使用POST接受表单值的create方法定义对象。

如果这是RoR,为什么要用java标记?粘贴控制器代码。@fge修订历史中没有标记@我很确定它就在那里,但是现在它不见了@真奇怪。通常会出现自我编辑。哦,的确如此。thanx,我知道它将为null,但不知道如何填充railsBest以在控制器中构建集合,然后将其传递给实例变量中的视图,例如@subjects。直接从视图调用模型违反了Rails的MVC模式。查看他的代码,他试图创建一本主题为类别的新书。他可以从给定的主题集合中选择主题。所以我不认为我的ans是错误的,除非他需要特定的主题。如果他需要特定的主题,那么他必须在控制器端创建一个集合@subjects,并在集合_select中使用该集合。@NikDP:you ans已经足够了,我只需要推送来填充空值field@gg_s:是的,我使用了控制器中的集合only@NikDP我从来没有说这是错误的,只是说这不是最佳实践。从视图调用模型会起作用,大多数反模式也会起作用,但不应该养成这样的习惯。
class Subject < ActiveRecord::Base
  attr_accessible :name
  has_many :book

end
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 --
<%= collection_select(:book,:subject_id,@subjects,:id,:name) %>
@subjects = Subject.all 
<%= collection_select(:book,:subject_id,Subject.all,:id,:name) %>
# 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