Problems with error_messages_for

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.

<% form_for :inventory_line_item, :url => { :action
=> :create_inventory_item, :id =>@product } do |form| %>
<%= error_messages_for ‘inventory_line_item’ %>

  1. Quantity <%= form.text_field 'quantity' %>
  2. Model

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

    @product   = Product.find(params[:id])
    

    line_item =
    InventoryLineItem.new(params[:inventory_line_item])
    line_item.date_added = Date.today
    line_item.added_by = member.name
    @product.inventory_line_items << line_item

    if @product.save
      flash[:notice] = 'Inventory successfully added'
      redirect_to :action => 'inventory', :id => @product
    end
      redirect_to :action => 'inventory', :id => @product
    

    end

On 2/20/07, jake [email protected] wrote:

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.

<% form_for :inventory_line_item, :url => { :action
=> :create_inventory_item, :id =>@product } do |form| %>
<%= error_messages_for ‘inventory_line_item’ %>

This line requires that you have the instance variable
@inventory_line_item
set and containing the object that you are interested in.

    > > end > > if @product.save > flash[:notice] = 'Inventory successfully added' > redirect_to :action => 'inventory', :id => @product > end > redirect_to :action => 'inventory', :id => @product > end

    line_item in the create action should be
    @inventory_line_item

    This should then see it

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.

#inventory controls
def inventory
@product = Product.find(params[:id])
@inventory_line_item = InventoryLineItem.new
end

def create_inventory_item
member = Member.find(session[:member_id])

@product   = Product.find(params[:id])

@inventory_line_item =
InventoryLineItem.new(params[:inventory_line_item])
@inventory_line_item.date_added = Date.today
@inventory_line_item.added_by = member.name
@product.inventory_line_items << @inventory_line_item
if @product.save
flash[:notice] = ‘Inventory successfully added’
end
redirect_to :action => ‘inventory’, :id => @product
end

On 2/20/07, jake [email protected] wrote:

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.

Hope that helps.

Thanks Daniel,

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