![](/img/trans.png)
[英]Trouble creating a new model object in Ruby on rails - params are a symbol?
[英]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.