Rails validations app specific problems

Hi all,

I have a unique workflow where it’s making the use of validations
difficult.

My app is for internal use in my company and I have a funky
implementation (inspired by the e-commerce Railscasts episodes) of a
cart. I have products which when I “add to cart” creates an order with a
state of “incomplete”. The product is a line item of my order. Hope that
makes sense.

Even though this is a little complicated, it works well (even though it
may not be the best implementation - suggestions are welcome though!)

Now the problem is when I go to checkout and complete my order I want to
validate certain things, like if my customer’s order number is present
(validates_presence_of). This should be easy, but since the order has
ALREADY been created I need to validate the presence of an attribute.

Also, I can’t just do this:

validates_presence_of   :customer_order_number, :on => :update

Because it generates a validation error on my ‘/cart’ page which is
where I can go and edit the product quantities for the client order.

I’m sure the solution lies in validates_presence_of with the :if
condition, but I just can’t figure out how to make it work.

Below is my related app code.

Orders Controller:

  def update
    @order = Orders.find(params[:id])

    respond_to do |format|
        if @order.update_attributes(params[:order])
          flash[:notice] = 'Your cart has been updated.'
          format.html { redirect_to( current_cart_url ) }
          format.xml  { head :ok }
        else
          format.html { redirect_to( current_cart_url ) }
          format.xml  { render :xml => @order.errors, :status =>
:unprocessable_entity }
        end
    end

  end

  def cart
    create_current_order unless current_order
    @order = current_order
  end

  def checkout
    @order = current_order
  end

  def complete_order
    @order = current_order

    respond_to do |format|
      if @order.update_attributes(params[:order])
        @order.complete #sets order completed at date and state to
'complete'
        flash[:notice] = 'Thank you! Your order is being processed.'
        format.html { redirect_to( products_path ) }
        format.xml  { head :ok }
      else
        format.html { render :action => 'checkout' }
        format.xml  { render :xml => @order.errors, :status =>
:unprocessable_entity }
      end
    end

  end

I know this is kind of tough to understand, please let me know if you
need more specifics or code. Thank you!

-Tony

On Apr 14, 4:47 pm, Tony T. [email protected] wrote:

validates_presence_of   :customer_order_number, :on => :update

Because it generates a validation error on my ‘/cart’ page which is
where I can go and edit the product quantities for the client order.

I’m sure the solution lies in validates_presence_of with the :if
condition, but I just can’t figure out how to make it work.

The :if option is just the name of an instance method that returns
whether the validation should run (eg if the status has some value or
something like that)

Fred

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs