Passing object from view to controller

2019-08-07 13:13发布

问题:

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.

回答1:

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.



回答2:

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