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
Related
In my view, I have a form that'll allow users to build their own car models. I have a field to choose car-parts and another field to add a color for it. For example, a user can choose "tire"=>"yellow". And there's a button to add new entries to the form.
For generating a form in the controller, I have read many articles on not to bind the entity directly to the form, instead use a DTO (data transfer object) to take care of the validation on the $request first. After a success validation may then have the DTO data pass to the entities.
On submit, my form is going to pass field names like "model_name", "part01", "color01", "part02", "color02" to the controller. From the symfony documentation it shows you how to make it works with the use of the "allow_add" option in the form builder. But here's the problem, that was based on the assumption that you're binding your form with the entities, but how about DTO? How do I put all those data into a DTO and tie it to the form and still able to do the validation?
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!
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.
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.
I have the following model:
Customer:
ID
Name
Address
Phone
Fax
I added an Edit view based on the above model from the controller. I modified the Edit view to only allow edit on the Phone and Fax field (deleted the rest). When I submit it I get an error. It works if I leave the Edit view untouched (5 fields). However I only want to allow change in the last 2 fields.
I am lost, please help. Thanks :)
If you are using the MVC ability to populate your entity/class i.e. your action sig looks like this:
ViewResult MyAction(MyObject object) {
...
Save(MyObject);
}
then you'll need to make sure you include the other field, non-editable, either as visible information or using Html.Hidden within the form scope to ensure you have a fully populated object. Remember, the web is stateless and the server has no idea which record you were editing unless it has the keys to do so retrospectively.
The other option would be to retrieve the original object (for which you'll still need the primary key) from the database, update the fields from your form data and then submit the changes. We'd need to know the specific error to be able to help further, the code you are using would also be a great help.
Without knowing more I would guess it has something to do with binding null to a non null property in your model. Can you give me more details on the model, the error.
If you are using the default mvc model binder then it will only bind the fields you submit. So either submit as hidden or dont use a model binder and manually map the variable from Request.Form into a copy of the model you pulled form the db.