I can't get my error messages to display. Maybe I don't yet understand
page life cycle or something? ROR is an elegant solution but the
transition from ASP.net has been painful to say the least. Code below
any help would be greatly appreciated.
class InventoryLineItem < ActiveRecord::Base
belongs_to :products
validates_presence_of :date_added, :added_by, :quantity, :unit_case, :case_price
validates_numericality_of :case_price, :message=>"Price must be a
valid decimal number"
protected
def validate
errors.add(:case_price, "should be at lease 0.01") if
case_price.nil? || case_price < 0.01
end
end
controller
def inventory
@product = Product.find(params[:id])
@inventory_line_item = InventoryLineItem.new
end
def create_inventory_item
member = Member.find(session[:member_id])
I can’t get my error messages to display. Maybe I don’t yet understand
page life cycle or something? ROR is an elegant solution but the
transition from ASP.net has been painful to say the least. Code below
I follow your logic, if I am using the instance variable it will not
get passed back to the page.
I made the following change but am still not getting any error
messaging? Could it be because I am using @product.save with <%=
error_messages_for 'inventory_line_item' %> or could it be that I am
instantiating and new inventory_line_item in inventory? Maybe the
errors are not bubbling up or something. I am confused for sure on
this one.
I follow your logic, if I am using the instance variable it will not
get passed back to the page.
That's not quite it. The error_messages_for method looks for an
instance variable with the name given in the argument.
In the docs
Returns a string with a div containing all of the error messages for
the objects located as instance variables by the names given. If more
than one object is specified, the errors for the objects are displayed
in the order that the object names are provided.
I made the following change but am still not getting any error
messaging? Could it be because I am using @product.save with <%=
error_messages_for 'inventory_line_item' %> or could it be that I am
instantiating and new inventory_line_item in inventory? Maybe the
errors are not bubbling up or something. I am confused for sure on
this one.
I believe the problem is that you are not saving @inventory_line_item
or at least calling the validation, and it is not saving as part of
the @product.save.
You will need to at least run the valid? method on the
@inventory_line_item or make sure that an attempt has been made to
save it.
Below is the code that fixed this issue. I had to check the valid
method first like you said and the key to making the validation
display was using the render action: => to redisplay the page like
this. When you redirect_to do you loose the state of the
error_message_for? I will need to read up on that. Thanks againf for
your help.
@inventory_line_item.added_by = member.name
if @inventory_line_item.valid?
@product.inventory_line_items << @inventory_line_item
if @product.save
flash[:notice] = 'Inventory successfully added'
redirect_to :action => 'list'
end
else
render :action => :inventory
end