2016-03-01 43 views
1

我有来电显示功能,为我的应用程序工作,但我目前难倒如何根据的params[:call_number_type]Rails的Ruby的逻辑基于帕拉姆值

params[:call_number_type]能值动态更新正确的列动态更新特定的列"alt_phone""cell_phone""office_phone"nil ...无零至默认为"alt_phone"将是理想的。

如果params[:call_number_type]具有该值,则上述每个字符串都对应于需要更新的列名称。

@contact = Contact.find(params[:contact_id]) 

if @contact.update(this_needs_to_be_the_right_column_key: params[:call_number]) 

上述UPDATE语句必须基于对params[:call_number_type]

值来动态创建如果有人可以帮助我走出这将是巨大的。谢谢。

回答

1

试试这个

if @contact.update(call_number_type => params[:call_number]) 
    ... 

private 

def call_number_type 
    params[:call_number_type].present? ? params[:call_number_type] : :alt_phone 
end 
+0

嘿万里感谢您的回答!我不知道这些字符串本身会代替密钥 – JerryA