Passing object from view to controller

2019-08-07 12:35发布

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.

2条回答
唯我独甜
2楼-- · 2019-08-07 13:10

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.

查看更多
Evening l夕情丶
3楼-- · 2019-08-07 13:10

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

查看更多
登录 后发表回答