Reputation: 6617
Hi i am facing this error , completely new to rails so cant figure out what is causing it
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>
my book model : 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
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 --
Upvotes: 7
Views: 36443
Reputation: 408
I recently ran into this issue. I had defined the object for the new
method (which called the form page using GET
) but had forgotten to define it for the create
method (which accepted form values using POST
), after defining it for both the new
and the create
issue got resolved.
Upvotes: 0
Reputation: 93
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
Upvotes: 0
Reputation: 14048
<%= collection_select(:book,:subject_id,@subjects,:id,:name) %>
Your @subjects
object is undefined. You need in your controller action for this page something that sets the contents of that variable, for example:
@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
).
Upvotes: 23