Passing object from view to controller - ruby-on-rails

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

Related

Access form_for object in corresponding controller method

I have a form_for, and I want to access variable of it in corresponding submit controller method. I'll explain what I mean:
I have list of objects rendered as follows:
<%= render medical_situations %>
I corresponding medical_situation.html.erb file I specify how each object looks like, where inside of each I have form_for as follows:
<%= form_for medical_situation, {url: :send_to_pool_medical_situations, method: :post, remote:true} do |f| %>
In corresponding controller method I want to access that particular medical_situation object. Is it possible. I know I could pass medical_situation_id to find appropriate object but I am interested can I do it without extra request and code. In my send_to_poo method I want to do update that object.
In corresponding controller method I want to access that particular
medical_situation object.
It's not possible in any way, because http is stateless. Each new request is handled independently from the previous ones. The application will remove all in-memory variables after response rendering and start new incoming request handling from scratch with new and fresh controller instance, so the medical_situations variable won't exist any more. The only way how to tell the application which object you want to display is to pass object id in request parameters. It will allow you to fetch this object from the database and display it from your controller.
what does it mean when they say http is stateless
https://stackoverflow.com/questions/13200152/why-say-that-http-is-a-stateless-protocol
No, I don't think you can send an object from a view page to your controller.
You have to pass the id of an object and again you have to find that object using the id from params in your controller .

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

Display AJAX controller action result using HtmlHelpers

I have a fairly complex object which has some C# code written to render it as HTML in various views.
There is also a view which can call an AJAX method of a controller, which returns the complex object serialized to JSON which should then be displayed.
This seems to leave me requriring complicated duplicate code to render the resulting JSON as HTML using Javascript/jQuery.
The obvious solution is to render the HTML in the controller action and return this from the AJAX call. However this seems in violation of the MVC pattern so not really a good option.
Is there a different way I can render the object returned from the AJAX method making use of the existing C# code?
Thanks.
Create a PartialView to which you render the object, and return that.
As mentioned, either create a PartialView user control and return that, which you can inject the HTML in nicely (returns the HTML as a string) or you can use a templating option in JQuery or something else to do the UI generation for you.
HTH.

Calling a controller from another

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'

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