2011-12-14 60 views
0

好吧,我有這個在我有什麼如何打開underscrore找到Ruby on Rails中模型

user.rb //用戶模式

OPTION = ['none', 'all', 'partial'] 
ANOTHER = ['true', 'false', 'neither'] 
YET_ANOTHER = ['something', 'another'] 

application_helper.rb

def my_method(information, name) 
    #this is where i need help 
    User::OPTIONS 

index.html.erb

my_method('information', :option) 
my_method('information', :another) 
my_method('information', :yet_another) 

我如何在my_method視圖助手抓住正確的數組因此,例如,如果我在my_method視圖助手在

my_method('information', :another) 

送我應該執行

​​

我在想我可以upcase,但如何執行此....

我試過下面

User::name.upcase 
NoMethodError: undefined method `name' for #<Class:0x000d001078cd9c8> 

回答

1

您只需要執行以下操作。

User.const_get(name.to_s.upcase) 

雖然這可行,請確保您沒有將用戶提交的參數傳遞到const_get中。這將是一個安全漏洞,允許用戶實質上執行意外的代碼。相反,你應該簡單地使用一個條件。

info = case params[:something] 
when 'foo'; User::FOO 
when 'bar'; User::BAR 
end 
+0

是有辦法做到這一點,如果所述第一部分是一個字符串也因此,如果i具有--------------信息=「用戶」 – Trace 2011-12-14 02:08:53