2013-06-06 22 views
6

Hi tôi đang phải đối mặt với lỗi này, hoàn toàn mới để ray hình nên không thể hiểu những gì đang gây ra nóphương thức không xác định `map 'cho nil: NilClass, nguyên nhân gì?

newBook.html.erb tôi

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

mô hình cuốn sách của tôi: 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 

mô hình đối tượng của tôi: subject.rb

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

end 

stack trace là:

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 -- 
+2

Nếu đây là ROR, tại sao thẻ có 'java'? – fge

+0

Dán mã bộ điều khiển của bạn. –

+0

@fge Không có thẻ [tag: java] trong lịch sử sửa đổi. (?) – Substantial

Trả lời

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

đối tượng @subjects của bạn là không xác định. Bạn cần trong hành động điều khiển của bạn cho việc này một cái gì đó trang mà bộ nội dung của biến đó, ví dụ:

@subjects = Subject.all 

Xem nguồn cho options_from_collection_for_select - Điều đầu tiên nó làm là một cuộc gọi đồ về thu truyền cho nó (trong trường hợp của bạn @subjects).

+0

thanx, tôi biết nó đã đến null, nhưng didnt biết làm thế nào để cư trú trong đường ray –

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

thanx để được giúp đỡ của bạn –

+1

Cố gắng thêm giải thích cho đoạn mã của bạn. – Matt

+0

Tốt nhất để xây dựng một bộ sưu tập trong bộ điều khiển ** **, sau đó chuyển nó vào khung nhìn trong một biến mẫu, ví dụ: '@ subject'. Gọi một mô hình trực tiếp từ chế độ xem vi phạm mẫu MVC của Rails. – Substantial

Các vấn đề liên quan