Calling a controller from another - ruby-on-rails

I want to call an action of a controller from another controller.
How can I do this?
Some years ago, there were compononents, but they are not available any more.
Best regards

You can't call an other controller's action method.
You have only two solutions.
Doing a redirect to the appropriate URL.
redirect_to '/'
But of course if you have datas from a form, you lose them.
Render the action from an other controller.
render 'controller/action'
You keep all your defined datas (params and everything).
But you need to do again all what the other controller would do.
One solution to avoid repeating code lines would be to have a library method defining every vars you need in your view and use this method in your two controllers.

redirect_to is the preferred way of doing this.
If you just want to render the other action's view with the current action's logic you can pass the view as an option to render.
render 'other_controller/action'

Related

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.

how can i call index action of default controller from my generated controller or how can i render index view from my controller?

I am not getting how can I render index view of default controller from my generated controller action.
Is there any way to do that in grails. Please let me know.
With advance thanks,
There's several way to do that. The simplest case is that you should name your view according to the controller/action that handle it.
For example, if your controller is HomeController with action index, your view should be /view/home/index.gsp. This's called Grails philosophy - Convention Over Configuration.
If you want to make an action render a specific view, you can use render command.
You should easily figure it out with the Grails document - Web layer.

Render Ajax question

Excuse me, Perhaps it is a foolish question.
there is a way for render a view (with action include) inside other view through ajax request?
I need to use ajax but i need that data don´t charge in principal action. I need that data charge in other action.
I don´t want to use a partial.
Thanks in advice
You can have an empty container div and fill it with the return data of a jQuery.ajax call to an action on one of your controllers with render :viewname, :layout => false

Get referring view in an Action - MVC2

This may be more of a best practice question.
I have three views (create/details/edit) that all link to their own results view (createResults/detailsResults/editResults). Each results view shares a partial with a results table on it.
When a user submits one of the three (c/d/e) views, should each results view have its own action, even tho the action will quite literally do the exact same thing (search on the information on the c/d/e view)? I'd rather not duplicate this code if not necessary.
Should I have one action, and pass in something that tells the action which results view to direct to? Is there an easy way to get the referring view in the action?
If you have 3 actions you don't need to duplicate code. Why not refactor the common code into a single private method on the controller, or perhaps even move it into an action filter?
I would make a single action with a string parameter containing the view name.
You can play with the routing table to make the URLs prettier.

Passing object from view to controller

Is there a way to pass a object, specifically a form_builder object used in a view, to an action in the controller? I am using a link_to_remote and want to update a variable in the controller and then add a new nested form element to my form via a render :update block.
If I pass it as a param, it gets converted string and I can't seem to successfully pass a locals hash to the controller either.
No, you cannot pass an object from the view to the controller.
All your logic should happen in the controller and model and at the end of the process the view renders the result in the browser or other places.
You can only use params, whatever you need to send to controller, must be serialized inside params. I'm sure you don't need to send whole form_builder object, send there just what is needed to construct new "subform"
BTW you should construct your views and controllers to work without any ajax, and then progressively enhance your forms and controllers to also support ajax. RJS and render :update is bad practice and should be avoided. find someone who understands javascript instead of writing pseudocode with Rails' RJS

Resources