2016-10-12 2 views
0

내 application_helper.rb에 변수로 전달 된 모델 이름을 허용하는 def가 필요합니다.레일 - 컨트롤러의 모델 이름에 변수를 사용하려면 어떻게해야합니까?

나는 내가 그것을 내가 할 노력했다 $ current_opportunity

<%# link_to "Lab", lab_path(get_id('Lab')), class: get_labs_class() %> 

의 내용을 기반으로 특정 레코드에 액세스하기 위해 전화 get_id 및 패스 '랩'을 한 노력 오전 탐색 볼 수 있습니다. 내가 그렇게 실행하려고하면

내 application_helper.rb 내가 즉 하드

model_ids = Lab.where("opportunity_id = ? ", $current_opportunity) 

에 데프을 변경하면 나는, 그러나

NoMethodError in Prospects#show 
Showing C:/Users/cmendla/RubymineProjects/product_development/app/views/shared/_tabs_nav.html.erb where line #61 raised: 

undefined method `where' for "Lab":String 
Trace of template inclusion: app/views/layouts/application.html.erb 

Rails.root: C:/Users/cmendla/RubymineProjects/product_development 

Application Trace | Framework Trace | Full Trace 
app/helpers/application_helper.rb:25:in `get_id' 
app/views/shared/_tabs_nav.html.erb:61:in `_app_views_shared__tabs_nav_html_erb__956794461_83328552' 
app/views/layouts/application.html.erb:113:in `_app_views_layouts_application_html_erb___468328524_46368744' 

를 얻을

def get_id(model) 
    model_ids = model.where("opportunity_id = ? ", $current_opportunity) 
    model_ids = model_ids.first 
    model_id = model_ids.id 
    model_id = model_id.to_i 
    return model_id 
    end 

있다 배선 랩, 모든 것이 작동합니다.

where 문에서 model 값을 대체 할 수있는 방법이 있습니까?

답변

1

시도 :

model_ids = model.constantize.where("opportunity_id = ? ", $current_opportunity) 

constantize는 문자

와 함께 작동
관련 문제