开发者

Variable symbol in Params object in rails

I have the following code:

@profile.update_attributes(params[:xxxx_profile])

where xxxx stands for either male or female. Basically the form submit passes either a set of female_profile[foo] or a male_profile[foo] and i want to change it accordingly. Assuming I have a string that can be inserted in li开发者_JS百科eu of xxxx, how do I dynamically create this symbol?

Thanks.


Try something like:

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

or, you should be able to pass in the string without converting to a symbol, because Rails uses a HashWithIndifferentAcceess for params: http://api.rubyonrails.org/classes/HashWithIndifferentAccess.html

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


Figured it out. Thought it might be useful for someone.

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


You could also do

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜