Passing object from view to controller

可紊 提交于 2019-12-11 07:48:56

问题


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



来源:https://stackoverflow.com/questions/846936/passing-object-from-view-to-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!