복붙노트

[RUBY-ON-RAILS] 레일 js.erb 파일이 방법을 찾을 수 없습니다 "렌더링"

RUBY-ON-RAILS

레일 js.erb 파일이 방법을 찾을 수 없습니다 "렌더링"

나는 자바 스크립트 ERB 파일에 다음 코드를 가지고 :

$(document).ready(function() {
  $("#workout-week").append(
    <%= escape_javascript(render :partial => "show_training_period", :locals => { :period => @period }) %>
  );
});

내가보기에 도착했을 때, 나는 다음과 같은 오류가 발생합니다 :

undefined method `render' for #<#<Class:0x00000004e21158>:0x00000005dbfe98>

부분이 존재하고, 로컬 변수는 비 전무하다. 이런 일이 이유 어떤 생각?

스택 추적 :

app/assets/javascripts/slider.js.erb:2:in `evaluate_source'
tilt (1.3.3) lib/tilt/template.rb:209:in `instance_eval'
tilt (1.3.3) lib/tilt/template.rb:209:in `evaluate_source'
tilt (1.3.3) lib/tilt/template.rb:144:in `cached_evaluate'
tilt (1.3.3) lib/tilt/template.rb:127:in `evaluate'
tilt (1.3.3) lib/tilt/template.rb:76:in `render'
sprockets (2.0.3) lib/sprockets/context.rb:175:in `block in evaluate'
sprockets (2.0.3) lib/sprockets/context.rb:172:in `each'
sprockets (2.0.3) lib/sprockets/context.rb:172:in `evaluate'
sprockets (2.0.3) lib/sprockets/bundled_asset.rb:171:in `build_dependency_context_and_body'
sprockets (2.0.3) lib/sprockets/bundled_asset.rb:135:in `dependency_context'
sprockets (2.0.3) lib/sprockets/bundled_asset.rb:194:in `build_dependencies_paths_and_assets'
sprockets (2.0.3) lib/sprockets/bundled_asset.rb:141:in `dependency_paths'
sprockets (2.0.3) lib/sprockets/bundled_asset.rb:61:in `mtime'
sprockets (2.0.3) lib/sprockets/asset.rb:58:in `block in encode_with'
sprockets (2.0.3) lib/sprockets/asset.rb:57:in `each'
sprockets (2.0.3) lib/sprockets/asset.rb:57:in `encode_with'
sprockets (2.0.3) lib/sprockets/bundled_asset.rb:43:in `encode_with'
sprockets (2.0.3) lib/sprockets/caching.rb:52:in `cache_asset'
sprockets (2.0.3) lib/sprockets/environment.rb:82:in `build_asset'
sprockets (2.0.3) lib/sprockets/base.rb:99:in `find_asset'
sprockets (2.0.3) lib/sprockets/environment.rb:72:in `find_asset'
sprockets (2.0.3) lib/sprockets/base.rb:110:in `[]'
sprockets (2.0.3) lib/sprockets/bundled_asset.rb:200:in `block in build_dependencies_paths_and_assets'
sprockets (2.0.3) lib/sprockets/bundled_asset.rb:194:in `each'
sprockets (2.0.3) lib/sprockets/bundled_asset.rb:194:in `build_dependencies_paths_and_assets'
sprockets (2.0.3) lib/sprockets/bundled_asset.rb:141:in `dependency_paths'
sprockets (2.0.3) lib/sprockets/bundled_asset.rb:61:in `mtime'
sprockets (2.0.3) lib/sprockets/asset.rb:58:in `block in encode_with'
sprockets (2.0.3) lib/sprockets/asset.rb:57:in `each'
sprockets (2.0.3) lib/sprockets/asset.rb:57:in `encode_with'
sprockets (2.0.3) lib/sprockets/bundled_asset.rb:43:in `encode_with'
sprockets (2.0.3) lib/sprockets/caching.rb:52:in `cache_asset'
sprockets (2.0.3) lib/sprockets/environment.rb:82:in `build_asset'
sprockets (2.0.3) lib/sprockets/trail.rb:102:in `find_asset_in_path'
sprockets (2.0.3) lib/sprockets/base.rb:101:in `find_asset'
sprockets (2.0.3) lib/sprockets/environment.rb:72:in `find_asset'
sprockets (2.0.3) lib/sprockets/base.rb:110:in `[]'
actionpack (3.1.0) lib/sprockets/helpers/rails_helper.rb:124:in `asset_for'
actionpack (3.1.0) lib/sprockets/helpers/rails_helper.rb:28:in `block in javascript_include_tag'
actionpack (3.1.0) lib/sprockets/helpers/rails_helper.rb:27:in `collect'
actionpack (3.1.0) lib/sprockets/helpers/rails_helper.rb:27:in `javascript_include_tag'
app/views/layouts/application.html.haml:7:in `_app_views_layouts_application_html_haml__2778507683501201875_51020080'
actionpack (3.1.0) lib/action_view/template.rb:144:in `block in render'
activesupport (3.1.0) lib/active_support/notifications.rb:55:in `instrument'
actionpack (3.1.0) lib/action_view/template.rb:142:in `render'
actionpack (3.1.0) lib/action_view/renderer/template_renderer.rb:52:in `render_with_layout'
actionpack (3.1.0) lib/action_view/renderer/template_renderer.rb:38:in `render_template'
actionpack (3.1.0) lib/action_view/renderer/template_renderer.rb:12:in `block in render'
actionpack (3.1.0) lib/action_view/renderer/abstract_renderer.rb:22:in `wrap_formats'
actionpack (3.1.0) lib/action_view/renderer/template_renderer.rb:9:in `render'
actionpack (3.1.0) lib/action_view/renderer/renderer.rb:36:in `render_template'
actionpack (3.1.0) lib/action_view/renderer/renderer.rb:17:in `render'
actionpack (3.1.0) lib/abstract_controller/rendering.rb:120:in `_render_template'
actionpack (3.1.0) lib/action_controller/metal/streaming.rb:250:in `_render_template'
actionpack (3.1.0) lib/abstract_controller/rendering.rb:114:in `render_to_body'
actionpack (3.1.0) lib/action_controller/metal/renderers.rb:30:in `render_to_body'
actionpack (3.1.0) lib/action_controller/metal/compatibility.rb:43:in `render_to_body'
actionpack (3.1.0) lib/abstract_controller/rendering.rb:99:in `render'
actionpack (3.1.0) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (3.1.0) lib/action_controller/metal/instrumentation.rb:40:in `block (2 levels) in render'
activesupport (3.1.0) lib/active_support/core_ext/benchmark.rb:5:in `block in ms'
/home/mike/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/benchmark.rb:310:in `realtime'
activesupport (3.1.0) lib/active_support/core_ext/benchmark.rb:5:in `ms'
actionpack (3.1.0) lib/action_controller/metal/instrumentation.rb:40:in `block in render'
actionpack (3.1.0) lib/action_controller/metal/instrumentation.rb:78:in `cleanup_view_runtime'
activerecord (3.1.0) lib/active_record/railties/controller_runtime.rb:24:in `cleanup_view_runtime'
actionpack (3.1.0) lib/action_controller/metal/instrumentation.rb:39:in `render'
actionpack (3.1.0) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (3.1.0) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
actionpack (3.1.0) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.1.0) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.1.0) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.1.0) lib/active_support/callbacks.rb:425:in `_run__4540075312289376507__process_action__207230359584297735__callbacks'
activesupport (3.1.0) lib/active_support/callbacks.rb:386:in `_run_process_action_callbacks'
activesupport (3.1.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.1.0) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.1.0) lib/action_controller/metal/rescue.rb:17:in `process_action'
actionpack (3.1.0) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.1.0) lib/active_support/notifications.rb:53:in `block in instrument'
activesupport (3.1.0) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.1.0) lib/active_support/notifications.rb:53:in `instrument'
actionpack (3.1.0) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.1.0) lib/action_controller/metal/params_wrapper.rb:201:in `process_action'
activerecord (3.1.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.1.0) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.1.0) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.1.0) lib/action_controller/metal.rb:193:in `dispatch'
actionpack (3.1.0) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.1.0) lib/action_controller/metal.rb:236:in `block in action'
actionpack (3.1.0) lib/action_dispatch/routing/route_set.rb:65:in `call'
actionpack (3.1.0) lib/action_dispatch/routing/route_set.rb:65:in `dispatch'
actionpack (3.1.0) lib/action_dispatch/routing/route_set.rb:29:in `call'
rack-mount (0.8.3) lib/rack/mount/route_set.rb:152:in `block in call'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:96:in `block in recognize'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:75:in `optimized_each'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:95:in `recognize'
rack-mount (0.8.3) lib/rack/mount/route_set.rb:141:in `call'
actionpack (3.1.0) lib/action_dispatch/routing/route_set.rb:531:in `call'
warden (1.1.0) lib/warden/manager.rb:35:in `block in call'
warden (1.1.0) lib/warden/manager.rb:34:in `catch'
warden (1.1.0) lib/warden/manager.rb:34:in `call'
actionpack (3.1.0) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.3.5) lib/rack/etag.rb:23:in `call'
rack (1.3.5) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.1.0) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.1.0) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.1.0) lib/action_dispatch/middleware/flash.rb:243:in `call'
rack (1.3.5) lib/rack/session/abstract/id.rb:195:in `context'
rack (1.3.5) lib/rack/session/abstract/id.rb:190:in `call'
actionpack (3.1.0) lib/action_dispatch/middleware/cookies.rb:326:in `call'
activerecord (3.1.0) lib/active_record/query_cache.rb:62:in `call'
activerecord (3.1.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:477:in `call'
actionpack (3.1.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (3.1.0) lib/active_support/callbacks.rb:392:in `_run_call_callbacks'
activesupport (3.1.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.1.0) lib/action_dispatch/middleware/callbacks.rb:28:in `call'
actionpack (3.1.0) lib/action_dispatch/middleware/reloader.rb:68:in `call'
rack (1.3.5) lib/rack/sendfile.rb:101:in `call'
actionpack (3.1.0) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
actionpack (3.1.0) lib/action_dispatch/middleware/show_exceptions.rb:47:in `call'
railties (3.1.0) lib/rails/rack/logger.rb:13:in `call'
rack (1.3.5) lib/rack/methodoverride.rb:24:in `call'
rack (1.3.5) lib/rack/runtime.rb:17:in `call'
activesupport (3.1.0) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.3.5) lib/rack/lock.rb:15:in `call'
actionpack (3.1.0) lib/action_dispatch/middleware/static.rb:53:in `call'
railties (3.1.0) lib/rails/engine.rb:455:in `call'
railties (3.1.0) lib/rails/rack/content_length.rb:16:in `call'
railties (3.1.0) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.3.5) lib/rack/handler/webrick.rb:59:in `service'
/home/mike/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
/home/mike/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
/home/mike/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'

해결법

  1. ==============================

    1.당신은 자산에서 렌더링 사용하려는. 불행하게도, 지금 가능하지 않다, 이것 좀 봐.

    당신은 자산에서 렌더링 사용하려는. 불행하게도, 지금 가능하지 않다, 이것 좀 봐.

  2. ==============================

    2.새로운 자산 파이프 라인으로, 고정 자산의 내부 (/ 응용 프로그램 | lib 디렉토리 | 공급 업체 / 자산 /) 일부 '정적'헬퍼를 사용할 수 있습니다 (예 : <% = asset_path "/ foo는 / 바"%>), 당신은 단지 한 번에 컴파일하기 때문에 하지 때마다 (프로덕션 예에 인 경우) 당신은 HTTP REQ합니다. 따라서 고정 자산의 내부 렌더링 (또는 일반 '동적'물건) 등의 사용 방법에 대한 이해되지 않는다.

    새로운 자산 파이프 라인으로, 고정 자산의 내부 (/ 응용 프로그램 | lib 디렉토리 | 공급 업체 / 자산 /) 일부 '정적'헬퍼를 사용할 수 있습니다 (예 : <% = asset_path "/ foo는 / 바"%>), 당신은 단지 한 번에 컴파일하기 때문에 하지 때마다 (프로덕션 예에 인 경우) 당신은 HTTP REQ합니다. 따라서 고정 자산의 내부 렌더링 (또는 일반 '동적'물건) 등의 사용 방법에 대한 이해되지 않는다.

    당신의 특별한 경우를 들어, HTML 내부에 직접 렌더링 모두 * .js.erb 조회수 (그 의지 '일치'컨트롤러 내부의 특정 작업) 또는 자바 스크립트를 사용할 수 있습니다.

  3. ==============================

    3.당신은 레일 렌더링 사용할 수 없습니다. 하지만 여전히 Erubis를 사용하여 파셜을 사용하는 것이 가능 :: Eruby 직접 렌더링.

    당신은 레일 렌더링 사용할 수 없습니다. 하지만 여전히 Erubis를 사용하여 파셜을 사용하는 것이 가능 :: Eruby 직접 렌더링.

    $(document).ready(function() {
        $("#workout-week").append(<%= escape_javascript(
            Erubis::Eruby.new(File.read(File.join(Rails.root, 'app/views',
            '_show_training_period.html.erb'))).result(period: @period)) %>);
    });
    

    또는 HAML에서

    $(document).ready(function() {
        $("#workout-week").append(<%= escape_javascript(
            Haml::Engine.new(File.read(File.join(Rails.root, 'app/views',
            '_show_training_period.html.haml'))).render(Object.new, period: @period)) %>);
    });
    

    ERB를 사용하여 몇 가지 이유로 스프로킷 렌더링을 나누기. 그리고 지역 주민 변수를 전달하는 바인딩 방법은 너무 밉다.

    그것은 주위에 방법입니다,하지만 자산 내부 파셜을 사용하지 않는 것이 좋습니다 것을 기억하는 것이 좋다. 더 좋은 방법은 HTML의 일부 코드 숨겨진 곳을 배치하고 자바 스크립트에서 사용하는 것입니다.

  4. from https://stackoverflow.com/questions/8370399/rails-js-erb-file-cannot-find-method-render by cc-by-sa and MIT license