gpt4 book ai didi

ruby-on-rails - undefined method `map' for nil :NilClass , 是什么原因造成的?

转载 作者:行者123 更新时间:2023-12-04 09:25:39 25 4
gpt4 key购买 nike

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

我的 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>

我的书模型: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

我的主题模型:subject.rb
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对象未定义。您需要在此页面的 Controller 操作中设置该变量的内容,例如:
@subjects = Subject.all 

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

关于ruby-on-rails - undefined method `map' for nil :NilClass , 是什么原因造成的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16960294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com