Rails 4 Single form-two actions -preview and assign - ruby-on-rails

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!

Related

Add Quantity Selected to Cart Instead of Just Updating

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.

Why do my viewmodel properties end up null or zero?

I'm working on my first MVC application, still figuring it all out.
I have a viewmodel which, at this point, is identical to my domain object. My controller builds the viewmodel and passes it to a view. The view only displays a some of the properties because I don't want primary and/or foreign keys displayed for the user yet, in the case of a primary key, I need to have the data in order to update / delete the database.
It appears that unless I use a viewmodel property in the view, it is set to default values (0 for numeric value types and null for reference types) when I pass the viewmodel back. Is this correct behavior?
I confirmed the viewmodel passed to the Edit view does contain all the properties (as I would expect).
The question - Once a view is rendered, what happens to the viewmodel? If my viewmodel contains properties that are not used in the view, do their values just disappear? For example, when I click the Edit actionlink to fire the Edit action on the controller, the viewmodel that gets passed to the action does not contain any properties unless they are visible on the screen. Why?
BTW, this is ASP.NET MVC 4 RC.
It appears that unless I use a viewmodel property in the view, it is
set to default values (0 for numeric value types and null for
reference types) when I pass the viewmodel back. Is this correct
behavior?
Yes, when you invoke a controller action you need to pass all the properties that you want to be bound in the request. So for example if you are using a html <form> to call the action you need to use input fields. You could use hidden fields but they must be present, otherwise nothing is sent to the controller action
The question - Once a view is rendered, what happens to the viewmodel?
It falls out of scope and is eligible for garbage collected.
If my viewmodel contains properties that are not used in the view, do their values just disappear?
Absolutely. But even if you use those properties inside the view they disappear. For example if you only display the values inside the view but do not use input fields to send them back to the server when the form is submitted they will be gone as well.
For example, when I click the Edit actionlink to fire the Edit action
on the controller, the viewmodel that gets passed to the action does
not contain any properties unless they are visible on the screen. Why?
Because the view model no longer exists. It's gone and garbage collected. That's how the HTTP protocol works. It's stateless. Nothing is persisted between the requests. You will have to include whatever properties you want to be populated in the request either as POST form values or as query string parameters if you are using action links or whatever.
If the user is not supposed to modify those values inside the view you could then simply pass an id to the controller action which will allow for this controller action to retrieve the model from wherever it is stored (a database or something) using this id.
If your properties are in any helper methods which generates the input HTML element inside a form, It will be available in your HTTPost action method when you submit the form. If you are simply displaying it in a div/span , it is not going to get you the property values. That is how MVC model binding works.
Expect the values in your HttpPOST action if you use these HTML helpers
#Html.TextBoxFor
#Html.DropDownFor
#Html.EditorFor
#Html.HiddenFor
Dont expect the values if you use these
#Html.DisplayFor

Passing data from one controller to another

I have a resource 'User'. In it's controller there is an action 'choose'. Index view is modified the way it is a form where you can choose some users. Pushing submit button invokes 'choose' action from controller. After some processing in this action there is an array #users containing selected users' ids.
This array should be somehow passed to another controller. Let's say there is another resource 'JobToDo'. In the controller there would be an action 'assign_workers'. When this action is called the following algorithm should be done:
call assign_workers
call index of the User
in the displayed view you choose some users
you click 'submit' and therefore invoke choose action (array #users is created)
array #users is than passed to assign_workers <--- and this is my problem
I want this choosing to be universal and to work no matter which action from which controller calls it. I don't want to add other 'logic' every time I decide to use this choosing in a new situation.
We better assume that the the array #users is quite big.
Is it possible? Or maybe my idea isn't a good one and I should do it another way - than how?
Thanks in advance
Bye
Without knowing the ins and outs of your application my first recommendation would be to use the choose action to display the form assign workers. Otherwise you will need to store it in the db or the session, or include them in the url as parameters.

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 Catching 'Save' POST with RenderPartial

I have a asp.net mvc page which renders a record from a database, it uses RenderPartial to call another view which renders an editable list of items related to that record.
My problem is I want a since save / submit button which not only saves changes made for that record but also changes made in the RenderPartial part... I have created a method accepting POST in the RenderPartials controller but it doesn't get called? Any ideas? Or am I using RenderPartial wrongly? I did it this way so that I have a controller that handles the subset of data
Update:
I don't think I've been clear enough:
Imagine a situation where you have a page that is filled with information from lots of different tables in a database... for example imagine you have a record of a person, and then you have all the links they have to Organisations that you want to list on the page, so the page contains:
Individual Name, Email, etc...
AND
Organisation Link 1
Organisation Link 2, etc... from a link table
Because of the amount of data I want to render of the page, I figured using different controllers to render each part would make sense.. but then when saving the data do I have to use just one controller method or can I call one controller to another... I only have one form and one 'save' button for the whole page
I hope this is clearer?
You don't need to have a specific controller for each controller, although I suspect you meant to say "I have created a method accepting POST in the RenderPartial's action ..."?
When you accept the default html helper commands, it can sometimes get confusing which action will be called. For a particular web page, the tag will determine where to POST values. So the action called will be dependent on the controller/action you specify in your Html.BeginForm call.
For example, here's a form we use from an ascx page:
<% using (Html.BeginForm("InvoiceDetail" //Action
, "CompanyInvoice" //Controller
, FormMethod.Post
, new { #id = "InvoiceForm", #autocomplete = "off" }))
{%>
A form can post to any action, in any controller. Not that you'd want to, but it's quite flexible. You can also have multiple forms in a single web page now, which is great for complex forms.

Resources