繁体   English   中英

轨道中Params对象中的变量符号

[英]Variable symbol in Params object in rails

我有以下代码:

@profile.update_attributes(params[:xxxx_profile])

其中xxxx代表男性或女性。 基本上表单提交传递一组female_profile[foo]male_profile[foo] ,我想相应地更改它。 假设我有一个可以插入的字符串代替xxxx,我该如何动态创建这个符号?

谢谢。

尝试类似的东西:

@profile.update_attributes(params["#{gender}_profile".to_sym])

或者,你应该能够传入字符串而不转换为符号,因为Rails使用了一个HashWithIndifferentAcceess for params: http ://api.rubyonrails.org/classes/HashWithIndifferentAccess.html

@profile.update_attributes(params["#{gender}_profile"])

弄清楚了。 认为它可能对某人有用。

@profile.update_attributes(params[(@sexstring + "_profile").to_sym])

你也可以这样做

@profile.update_attributes(params[:"#{gender}_profile"])

暂无
暂无

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

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