2013-10-02 4 views
1

난 레일 프로젝트가있다. application.html.slim에서레일스보기에서 content_for 메소드가 정의되지 않은 이유는 무엇입니까?

I가 가지고 또한 상기 제어부는 컨텐츠가 대응하는 뷰와 방법 new을 갖는다

def javascript(*files) 
    content_for(:html_head) do 
     javascript_include_tag(*files) 
    end 
    end 
    helper_method :javascript 

: 메서드가 제어기 SearchController에서는

doctype html 
html 
    head 
    title RailsMongoidPj 
    = stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true 
    = javascript_include_tag "application", "data-turbolinks-track" => true 
    = csrf_meta_tags 
    meta content="width=device-width, initial-scale=1.0" name="viewport" 
    = yield(:html_head) 
    body 
    = yield 

:

= javascript('/vendor/assets/javascript/handelbars.runtime.js') 

- content_for :html_head do 
    script#entry-template type="text/x-handlebars-template" 
    | template content 

n 오류 :

undefined method `content_for' for #<SearchFlickrController:0x007f8e4f2bae78> 

Extracted source (around line #3): 

1/search flickr api page 
2 
3 = javascript('/vendor/assets/javascript/handelbars.runtime.js') 
4 
5 - content_for :html_head do 
6 script#entry-template type="text/x-handlebars-template" 

이유가 content_for 인 이유는 무엇입니까? 표준 rails 방법이 아닌가요?

답변

2

content_for은 도우미 방법입니다. 컨트롤러가 아닌보기에서 content_for을 사용합니다. 따라서 content_for를 사용하는 방법에 대한 올바른 장소는 도우미 (예를 들어 app/helper/search_helper.rb)입니다 :

module SearchHelper 
    def javascript(*files) 
    content_for(:html_head) do 
     javascript_include_tag(*files) 
    end 
    end 
end 
+0

당신이 일을 여전히 볼 외부 방법을 가지고 만들기 위해 무엇을 알 수 있습니까? 나는이 해결책을 여기에서 찾았다 : http://stackoverflow.com/questions/6571753/rails-3-1-asset-pipeline-how-to-load-controller-specific-scripts. – static

+0

SearchHelper에 메서드를 추가했는데 이제 작동합니다. – static