Passing data from one controller to another - ruby-on-rails

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.

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!

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.

How do I pass a list of integers to an MVC action?

I have an action that depends on a list of integers. My first instinct was to simply declare the action with a List.
I tried declaring the action in the controller as:
public ActionResult EditMultiple(List<int> ids)
and in my View call like so:
<%= Html.ActionLink("EditMultiple", "EditMultiple", new { ids = new List<int> {2, 2, 2} })%>
Although it compiles the List is empty when I put a breakpoint in the action. Anybody know why or have an alternate approach?
Adding more detail about the scenario:
I'm trying to "Edit" multiple entities at the same time. I'm already at the point where I have an application that allows me to create/edit/view information about books in a library. I have a partial view that allows the user to edit information about a single book and save it to the database.
Now I'd like to create a View which allows the user to edit the information about multiple books with a single submit button. I've created an action EditMultiple which just renders the partial for each book (my model for this view is List) and adds the submit button afterwards.
Yes. The default model binder can bind "ids" to any ICollection. But you have to submit multiple parameters with the same name. That eliminates using the helper method "ActionLink". You can use url helper Action and append the ids to the link like so:
Test link
Here's the link from Haack's block.
I dont think
new List<int> {2, 2, 2}
gets properly converted into an POST that is bindable, you want to send
Ids=1&Ids=2&Ids=3
a comma separate list may also work, im not sure, i don't use the crappy default modelbinder.
Why are you doing that anyway? I hope thats pseudocode for something else...

How to design a complex form that requires search results from a different MVC controller

I want to create a form for creating an order. So I have an 'Order' controller. One of the first steps will be to select an existing person to send this order to.
I'm thinking the Create() action method on OrderController will put a new Order object in session. Then I'll need a link that will redirect to another controller, then return a customerID int to this Create() method.
I will have either a SearchController with a FindCustomer() action method, or a Search() action method on the CustomerController.
I have tried the first way. But what I am doing looks pretty messy. Especially considering that I'll need to do this at least one more time on this form, redirecting to the ItemsController to return items to add to the order.
What's the best way to design communication like this between controllers?
I'm not sure why you think you need other controllers for this. In your GET Create action, put the available Customers and Items in to ViewData, and then in your view put some controls for the user to select values.
Then they will be POSTed to your POST Create action, and you can bind & save it in your Order object. You could have a separate action for adding Items to the Order if that gets too complex. But it could still be on the same OrdersController.

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