- Where Developers Learn, Share, & Build Careers
This one is confused with me. I'm still a newbie with tracks so it can be easy.
Status:
- I can add items to the cart, no problem Everything works fine The current_cart method resolves each request for the same cart.
- But as soon as I remove a line item from the cart, it works, the line item is removed, but "variable [session_ cart]" becomes zero, and there is a new car When the current card is called next time, it was made.
I am using Devise so I am not sure whether to do something with it, or perhaps the line item removal method
The question is, is there anybody It helps to understand why the session variable is being cleared after removing the line_item?
I have a train application, with the sprinting web development with the following railing. First of all, the code for the application controller has been retrieved to retrieve the current cart:
private def current_cart Checkout :: Cart.find (session [: cart_id]) Rescue ActiveRecord :: RecordNotFound Cart = Checkout :: Cart. Session session [: cart_id] = cart.ind cart
Now the code for my line item controller
Class checkout: LineItemsController & lt; ApplicationController
# post / checkout / line_items # post /checkout/line_items.json def
@cart = current_cart product = list :: Product.find (parameter [: Product_id]) apparel_event_log.info (product.to_json) @checkout_line_item = @ cart.line_items.find_or_initialize_by_sku_id (product.part_number) new_quantity = @ checkout_line_item.quantity || 0 @ checkout_line_item Quantity = New_Contact + 1 @ checkout_line_item. Line_time_description = product.name @ checkout_line_item.image = product. Primary_Engase answer_to dot | Format | If @checkout_line_item.save format.html {check_out_line_item.cart @ redirect_to, notice: 'line item was created successfully.' @checkout_line_item, position ::} {format.json json render: created, location: @checkout_line_item} else format.html {render action: 'new'} {format.json json render: @ checkout_line_item.errors, status: : Unprocessable_entity} end-of-end
#delete / checkout / line_item/1 # delete / check / line_its / 1.jasan df checkout :: lineItem.delete (consultation [: id])
Answer_ | Format | Format.html {redirect_to current_cart, notice: 'line item was deleted.' } Format.json {head: no_content} end-end
and code for line item model
class checkout :: line item & Lt; ActiveRecord :: Base before_save: default_values attr_accessible: customer_update_date ,: inventory_status ,: line_item_description ,: line_item_size ,: line_item_tagline ,: line_item_total ,: image ,: quantity ,: sku_id ,: style_id,: tax, tax_code ,: timestamp,: unit_pres ,: Cart,: product related_to: cart related_to: product ,: class_name = & gt; 'List :: Products' has_one: Image ,: class_name = & gt; Assets: Medium 'def defaults_value Rail Logger Debug {"entering default value"} self.quantity || = 0 End End
First you should bring the argument into the model:
category cart & lt; & Lt; Adding a AR (product_id) product = list :: Product.find (product_id) if item = self.line_items.find_or_initialize_by_sku_id (product.part_number) item.quantity = (item.quantity || 0) + 1 item.line_item_description = product. Name item.image = product.primary_image Return items.Save end and class checkout :: LineItemsController & lt; ApplicationController # POST / Checkout / Line_Type # POST /checkout/line_items.json def Make @cart = current_cart apparel_event_log.info (product.to_json) response_to do format | If @checkout_line_item = @ cart.add (params [: product_id]) format.html {redirect_to @ checkout_line_item.cart, notice: 'line item was created successfully.' @checkout_line_item, position ::} {format.json json render: created, location: @checkout_line_item} else format.html {render action: 'new'} {format.json json render: @ checkout_line_item.errors, status: : Unprocessable_entity} End End End
Second You should get started:
Start the personal Draft Current Startout Checkout: Cart.find (session [: cart_id ]) Rescue Activacord:: RecordNotFound Cart = Checkout :: Cart.ScreateSession [: cart_id] = Cart.Id Cart End And
Comments
Post a Comment