Please help, Even if you''re not reading the book, maybe you can help me. I need to figure out why validation isn''t working, pages 104-105 in RoR book Beta 3. ----- Model: Order.rb ------- class Order < ActiveRecord::Base has_many :line_items belongs_to :order validates_presence_of :name, :email, :address, :pay_type PAYMENT_TYPES = { "Check" => "check", "Credit Card" => "cc", "Purchase Order" => "po" } end ----- View: checkout.rhtml ------ <% @page_title = "Checkout" -%> <%= start_form_tag(:action => "save_order") %> <%= error_messages_for("order") %> <table> <tr> <td>Name:</td> <td><%= text_field("order", "name", "size" => 40 ) %></td> </tr> ... ---- Layout: store.rhtml ------ <head> <title>Pragprog Books Online Store</title> <%= stylesheet_link_tag "scaffold", "depot", :media => "all" %> ... ---- Sql for table order ------ create table orders ( id int not null auto_increment, name varchar(100) not null, email varchar(255) not null, address text not null, pay_type char(10) not null, primary key (id) ); ---------------- Can anyone see anything that would stop validation from working? I''m quite baffled on this one. BTW I''m running them most recent version of everything on two different windows PCs, who both get the same error. Thanks! -Gregg
On Jun 30, 2005, at 4:26 PM, Gregg wrote:> ----- Model: Order.rb ------- > class Order < ActiveRecord::Base > has_many :line_items > belongs_to :order >Although this probably isn''t causing the problem, I wonder if the belongs_to :order should be in the LineItem model instead? Duane Johnson (canadaduane)
What do you mean by "validation isn''t working"? On 6/30/05, Gregg <patched-i8bqrNpN1PPCXmymsgaQcQ@public.gmane.org> wrote:> Please help, > > Even if you''re not reading the book, maybe you can help me. I need to > figure out why validation isn''t working, pages 104-105 in RoR book Beta 3. > > ----- Model: Order.rb ------- > class Order < ActiveRecord::Base > has_many :line_items > belongs_to :order > validates_presence_of :name, :email, :address, :pay_type > PAYMENT_TYPES = { > "Check" => "check", > "Credit Card" => "cc", > "Purchase Order" => "po" > } > end > > ----- View: checkout.rhtml ------ > > <% @page_title = "Checkout" -%> > <%= start_form_tag(:action => "save_order") %> > <%= error_messages_for("order") %> > <table> > <tr> > <td>Name:</td> > <td><%= text_field("order", "name", "size" => 40 ) %></td> > </tr> > ... > > ---- Layout: store.rhtml ------ > > <head> > <title>Pragprog Books Online Store</title> > <%= stylesheet_link_tag "scaffold", "depot", :media => "all" %> > ... > > ---- Sql for table order ------ > > create table orders ( > id int not null auto_increment, > name varchar(100) not null, > email varchar(255) not null, > address text not null, > pay_type char(10) not null, > primary key (id) > ); > > ---------------- > > Can anyone see anything that would stop validation from working? I''m > quite baffled on this one. BTW I''m running them most recent version of > everything on two different windows PCs, who both get the same error. > > Thanks! > > -Gregg > > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Gregg wrote:> Please help, > > Even if you''re not reading the book, maybe you can help me. I need to > figure out why validation isn''t working, pages 104-105 in RoR book Beta 3. > > ----- Model: Order.rb ------- > class Order < ActiveRecord::Base > has_many :line_items > belongs_to :order"belongs_to :order" shouldn''t in Order (it should be in LineItem).> validates_presence_of :name, :email, :address, :pay_type > PAYMENT_TYPES = { > "Check" => "check", > "Credit Card" => "cc", > "Purchase Order" => "po" > } > end > > ----- View: checkout.rhtml ------ > > <% @page_title = "Checkout" -%> > <%= start_form_tag(:action => "save_order") %> > <%= error_messages_for("order") %>The book asks you to put <%= error_messages_for("order") %> at the start of the file. This probably won''t make a difference now, but when you add the display of the cart, which is before the form, you will want the error messages to be above that. So I would move it to before the start_form_tag. That''s consistent with the finished code for the beta 2 version. (By the way, the code for the beta 3 book is downloadable - it would be worth having that to cross-check against.)> [ snip ] > > Can anyone see anything that would stop validation from working? I''m > quite baffled on this one. BTW I''m running them most recent version of > everything on two different windows PCs, who both get the same error.What does your save_order method look like, in StoreController? It''s essential that you call render(:action => ''checkout'') if @order.save fails (i.e. returns false). Justin
I''m having a similar problem, and it''s got me stumped. It seems to be happening with form fields that reference a separate table. If I take the input field loops for external tables out of the view, the validations work fine. If I leave them in, I get the following message: NoMethodError in Admin#create Showing /admin/_form.rhtml where line #22 raised: WARNING: You have a nil object when you probably didn''t expect it! Odds are you want an instance of Array instead. Look in the callstack to see where you''re working with an object that could be nil. Investigate your methods and make sure the object is what you expect! Extracted source (around line #22): 19: 20: <p>Classification<br /> 21: <select id="employee_classification_id" name="employee[classification_id]"> 22: <%= options_from_collection_for_select @classifications, "id", "class_name", @employee.classification_id %> 23: </select> 24: </p> 25: