简体   繁体   English

将对象从视图传递到控制器

[英]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? 有没有办法将对象(特别是视图中使用的form_builder对象)传递给控制器​​中的操作? 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. 我正在使用link_to_remote并想要更新控制器中的变量,然后通过render:update块将新的嵌套表单元素添加到我的表单中。

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. 如果我将它作为参数传递,它将转换为字符串,我似乎无法成功地将本地哈希传递给控制器​​。

You can only use params, whatever you need to send to controller, must be serialized inside params. 你只能使用params,无论你需要发送给控制器,都必须在params内序列化。 I'm sure you don't need to send whole form_builder object, send there just what is needed to construct new "subform" 我确定你不需要发送整个form_builder对象,只需要发送新的“子表单”即可。

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. 顺便说一句,您应该构建您的视图和控制器,以便在没有任何ajax的情况下工作,然后逐步增强您的表单和控制器以支持ajax。 RJS and render :update is bad practice and should be avoided. RJS和渲染:更新是不好的做法,应该避免。 find someone who understands javascript instead of writing pseudocode with Rails' RJS 找一个懂得javascript而不是用Rails的RJS编写伪代码的人

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. 您的所有逻辑都应该在控制器和模型中发生,并且在流程结束时,视图会在浏览器或其他位置呈现结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM