Add Quantity Selected to Cart Instead of Just Updating - ruby-on-rails

I have an index page with items and a quantity select form.
The form works for create (a.k.a. first click). The problem is, if I click it again, it's just updating the cart instead of adding that quantity to the line_item. If this was my only problem I would be able to solve it myself.
The real problem is that I have another form that updates right before checkout. It looks like this:
I want this to be the master quantity control, so whatever goes in that form is what the quantity will be for update. But for the first images, I want that quantity to add to the quantity of the #line_item, so I can't just make a method that just adds the new and old quantities together, which is what I started doing until I realized I wouldn't be able to do that.
Do I need to make a new action in the controller?
What would be the work around for this?

Looking at your question
The problem is, if I click it again, it's just updating the cart instead of adding that quantity to the line_item
The problem is your submit button Add to Cart which takes it to the create action of your cart and hence creating a new item inside your cart
Do I need to make a new action in the controller?
My answer would be yes. You need to make a new action with post route request(to find that item) and then update its quantity inside that action.
What would be the work around for this?
If you look at your button or rather i should say form for creating a new item then it's the path or url part in your form which takes it to your method inside your controller. If you change its url then it will take it to your custom method.
Fix
All you need is some js magic to dynamically change the url or your form after a user has clicked on your Add to Cart button. Something like:
$(document).on("click","your_button_class",function(){
$(this).closest("your_form_class").attr("action","path_of_new_method");
});
You will also have to supply your items id by this form by adding a hidden field or something and then find that item inside controller method with that id to update its quantity.
Note: You need to call this js after your form is submitted and a new item is already created else it can trouble you.

Related

Rails 4 Single form-two actions -preview and assign

I have a form which has two buttons
One will assign the form, i mean create an entry in corresponding table after validation and redirect to another page
Second will give a preview of the values submitted on the form.For the preview screen we need some calculations so that an action to be called from controller and respective page will be loaded.
This page should seen on the same form if i clicked preview before assigning the form
How i would address this is to change your new action so that it takes the same params as the create action, and makes the object but doesn't save them. This means that if you call the new action with the params it will reload the form with the built but not saved objects' data displayed in it.
The two buttons will need to change the form's action attribute so that it submits to the create or the new action as appropriate.
This is a general answer, as your question is quite general!

rails_admin duplicate action

I want to make custom action for rails_admin which copies whole data from some item(except id) in database into form that creates new item(rails_admin "new" action).
I want't to allow users create items with no necessity of filling all field in form.
I haven't tried it myself, but this library appears to add a duplicate action to rails_admin:
https://github.com/dalpo/rails_admin_clone

What is the best practices to track action id in controller for saved form?

Wasnt sure how to word the question, but this is the scenario:
the view is a data entry form eg http://127.0.0.1/User/AddEdit/
so edit the user I have an ID: http://127.0.0.1/User/AddEdit/7838fd9c-425c-4c98-b798-771bba10d9c1
This ID gets the data to populate the form values in a ViewModel, which populates the form
I am using jquery/ajax to save the form, which returns a Json result, indicating ok/error etc
In the View, I get the ID and use this in a hidden field which is set via jquery when the page loads and when the form is saved via ajax.
This seems a bit clunky, how do others do this?
in my opinion best solution is to create a partial view with all the fields and use it on add and edit view which are separate actions in controller. after you create user you can redirect to action edit. if you must / like use ajax you can reload div with form (change from user/add to user/edit/1). i might be wrong but i never see a code or example with one action in controller for add and edit.

Asp.net MVC eCommerce app - Add To Cart implementation

Let say I am rendering list of products with Add To Cart link / button next to each.
I also have a CartController with AddToCart Action that accept a parameter of Product type.
After product is added to cart user should stay on the same page (Product list) - not to be redirected to Cart or something.
I am rendering Cart summary partial view that should update itself.
So my question is about implementation of the Add To Cart link / button.
Should I use: Html.ActionLink(...)
or Html.BeginForm() and Submit button.
Maybe there is other ways...
How do I send Product info in each case?
Thanks
I suggest using a jQuery.post() request to your CartController.AddToCart. Make AddToCart a partial view. And then show a modal popup (div on top of your page) that shows that the product was added to the cart...then do a fade out!
Keep it simple.
Now keep in mind that some of your users won't be able to support jquery/javascript. So make the initial add to cart button post to an add to cart page (not partial page..full page)...which can return them to the original page. Then spot weld your jquery function on top of the add to cart button. This way you cover both worlds nicely.
Take a look at the concept of unobtrusive javascript/jquery.
The way I do it is to have a form for each add button (with maybe the quantity also), since you want your AddToCart action only receive POST actions anyway, that way you process all the add to cart logic and then redirect to your main catalog view (or something like that :)
Again I'd take into consideration what happens if the user doesn't have javascript. It's not very likely these days, but do you want to lose a sale just because someone accidently turned off javascript?
So what I'd do is create the somewhat typical AddToCart link (<a class="add" href="/Shop/AddToCart/5">). The AddToCart controller would then do it's stuff and redirect back to the product listing page (you might have to pass a parameter to specify which page to go back to).
So that's the first step to consider -- javascript turned off. Now you can think about how to do it with javascript.
Capture the click event ( $('a.add').click(...) ) and then you can do two things. One would be to call the URL (you can get it out of the event object) and then separately update the cart. You can also add a parameter to the URL so that it displays the cart partial view, doing something like (written from memory):
$('a.add').click(function(event) { $('#cart').load(event.target.attr('href') + '&showcart=1');
James

How do I pass a parameter to a form in Rails that persists on validation error?

I'm attempting to create a hierarchal menu system in rails.
My menu model is in a tree data structure, and I need to pass the parent information to the new action. The best way i found to do this was to pass the parent's id as a querystring. This works, unless validation blocks creation. The problem is that after the new view is rendered, the parameter is no longer present in the url.
Am i going the wrong way with this? Should I just be persisting the value in the session? Should i create a dropdown to pick the parent item (I'd prefer to just let the user click "Add new item here"). This must be a common issue, but I havent been able to find antything relevant.
Update: To clarify a question, I tried doing the following to pass the parameter when the view is rendered in the failure case, but the url has already been set at that point (we've already posted to the controller's base url).
format.html { render :action => "new", :parent_id=> params[:parent_id] }
I need another way to pass the parameter.
I figured it out. I just added a hidden field to hold the parent_id. When the browser requests the new form, i put the parent_id into the model object, and it's persisted on psotback.
Not 100% clear on what you are trying to achieve.
After the operation fails validation, you render the view again?
Can you not just pass the parent_id to this view as a parameter?

Resources