Created
November 8, 2013 12:04
-
-
Save svisamsetty/7370079 to your computer and use it in GitHub Desktop.
Example of how an action which returns JSON response should look like in Rails.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def additemtoorder | |
@result = Hash.new | |
@result['status'] = true | |
@result['error_messages'] = [] | |
@result['success_messages'] = [] | |
@order = Order.find(params[:id]) | |
@product = Product.find(params[:productid]) | |
@skus = ProductSku.where(:product_id=>@product.id).where(:purpose=>'primary') | |
if @skus.length > 0 | |
@orderitem = OrderItem.new | |
@orderitem.price = params[:price] | |
@orderitem.qty = params[:qty] | |
@orderitem.row_total = params[:price].to_f * params[:qty].to_f | |
@orderitem.sku = @skus.first.sku | |
@order.order_items << @orderitem | |
if !@order.save | |
@result['status'] &= false | |
@result['error_messages'].push("Adding item to order failed") | |
else | |
@result['success_messages'].pushd("Added item to order successfully") | |
end | |
else | |
@result['status'] &= false | |
@result['error_messages'].push("Could not find any SKU with product id:"+@productid) | |
end | |
respond_to do |format| | |
format.html # show.html.erb | |
format.json { render json: @result } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment