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:
[code]
validates_presence_of :customer_order_number, :on => :update
[/code]
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:
[code]
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
[/code]
I know this is kind of tough to understand, please let me know if you
need more specifics or code. Thank you!
-Tony
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.