Hi,
Sorry I didn’t post the right thing. I have two tables category and
book. I have a hidden category_id field in the create book page. So by
right if I submit an empty create book form,
an error message box will show on the top of the page displaying my
errors. But instead, I got the following error. Where did it go wrong.
Please help. Have been stuck for days.
I have also tried to use form_for with render partial but I can’t submit
my form. It just redirect back to the same page.
Thanks
…category table…
id integer (primary key)
cat_name varchar
validates_presence_of :name
…book table…
id integer(primary key)
name varchar
author varchar
price float
category_id integer(foreign key)
validates_presence_of :name, :author
…category model…
class Category < ActiveRecord::Base
has_many: books
end
…book model…
class Book < ActiveRecord::Base
belongs_to: category
end
…book.rhtml (view)…
<%= error_messages_for ‘book’ %>
Create New Book
<% form_for :book, :action => :save_book, :html => {:id => :book} do |f|
%>
<%= f.hidden_field :category_id, :value => @category.id %>
<%= button_to “Create”, :action => :save_book, :id => :book %>
<% end %>
…
…_form.rhtml…
Name: <%= f.text_field :name %>
Address: <%= f.text_field :author %>
Phone Number: <%= f.text_field :price %>
..................................
…error message…
Called id for nil, which would mistakenly be 4 – if you really wanted
the id of nil, use object_id
…