Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syntax Error in Jbuilder File When Declaring Constants #565

Open
Shao-Ting-Fong opened this issue May 7, 2024 · 2 comments
Open

Syntax Error in Jbuilder File When Declaring Constants #565

Shao-Ting-Fong opened this issue May 7, 2024 · 2 comments

Comments

@Shao-Ting-Fong
Copy link

Description

I'm encountering a syntax error in my Rails project when declaring constants in a .jbuilder file. Initially, I tried to declare a hash constant named PARTIAL_ROUTER for routing partial paths, which caused an error. Surprisingly, even a simple string constant like APPLE = "123" triggers the same error. This happens even though the syntax for declaring these constants seems correct. The error specifically occurs in the Jbuilder template while rendering JSON structures that involve partials.

Steps to Reproduce

  1. Declare a constant at the beginning of a Jbuilder file.
  2. Use this Jbuilder file to render complex JSON structures involving partials.
  3. Load the view that triggers this Jbuilder template.

Expected Behavior

The Jbuilder file should render the JSON structure without any syntax errors, and the constant should be recognized without causing issues.

Actual Behavior

When the Jbuilder file is triggered, Rails raises a syntax error related to the part of the template that renders partials. This suggests there might be an issue with how constants are parsed or rendered in the context of Jbuilder templates.

Code Snippet

# Declaring a constant in the Jbuilder file
APPLE = "123".freeze

# JSON structure in Jbuilder
json.seriesData do
  json.array! @indicators do |indicator|
    partial_path = File.join(
      controller_path,
      indicator.class.name.underscore.pluralize.delete_prefix('indicator/')
    )

    json.data do
      json.partial! File.join(partial_path, 'data_series'), indicator: indicator
    end

    json.partial! File.join(File.dirname(partial_path), 'info'), indicator: indicator
  end
end

Error Message

ActionView::SyntaxErrorInTemplate at /api/v2/indicators
=======================================================

Encountered a syntax error while rendering template: check 
APPLE = "123".freeze
json.seriesData do
  json.array! @indicators do |indicator|
    partial_path =
      File.join(
        controller_path,
        indicator.class.name.underscore.pluralize.delete_prefix('indicator/')
      )
    json.data do
      json.partial! File.join(partial_path, 'data_series'), indicator: indicator
    end
    json.partial! File.join(File.dirname(partial_path), 'info'), indicator: indicator
  end
end

> To access an interactive console with this error, point your browser to: /__better_errors


actionview (7.0.8.1) lib/action_view/template/error.rb, line 161
----------------------------------------------------------------

  156         # Override to prevent #cause resetting during re-raise.
  157         attr_reader :cause
  158   
  159         def initialize(template)
  160           super($!.message)
> 161           set_backtrace($!.backtrace)
  162           @cause = $!
  163           @template, @sub_templates = template, nil
  164         end
  165   
  166         def file_name

Full backtrace
--------------

 - actionview (7.0.8.1) lib/action_view/template/error.rb:161:in `initialize'
 - actionview (7.0.8.1) lib/action_view/template/error.rb:235:in `initialize'
 - actionview (7.0.8.1) lib/action_view/template.rb:312:in `rescue in compile'
 - actionview (7.0.8.1) lib/action_view/template.rb:302:in `compile'
 - actionview (7.0.8.1) lib/action_view/template.rb:256:in `block (2 levels) in compile!'
 - activesupport (7.0.8.1) lib/active_support/notifications.rb:208:in `instrument'
 - actionview (7.0.8.1) lib/action_view/template.rb:357:in `instrument'
 - actionview (7.0.8.1) lib/action_view/template.rb:255:in `block in compile!'
 - actionview (7.0.8.1) lib/action_view/template.rb:247:in `compile!'
 - actionview (7.0.8.1) lib/action_view/template.rb:156:in `block in render'
 - activesupport (7.0.8.1) lib/active_support/notifications.rb:208:in `instrument'
 - actionview (7.0.8.1) lib/action_view/template.rb:361:in `instrument_render_template'
 - actionview (7.0.8.1) lib/action_view/template.rb:155:in `render'
 - actionview (7.0.8.1) lib/action_view/renderer/template_renderer.rb:65:in `block (2 levels) in render_template'
 - activesupport (7.0.8.1) lib/active_support/notifications.rb:206:in `block in instrument'
 - activesupport (7.0.8.1) lib/active_support/notifications/instrumenter.rb:24:in `instrument'
 - activesupport (7.0.8.1) lib/active_support/notifications.rb:206:in `instrument'
 - actionview (7.0.8.1) lib/action_view/renderer/template_renderer.rb:60:in `block in render_template'
 - actionview (7.0.8.1) lib/action_view/renderer/template_renderer.rb:79:in `render_with_layout'
 - actionview (7.0.8.1) lib/action_view/renderer/template_renderer.rb:59:in `render_template'
 - scout_apm (5.3.1) lib/scout_apm/instruments/action_view.rb:149:in `render_template'
 - actionview (7.0.8.1) lib/action_view/renderer/template_renderer.rb:11:in `render'
 - actionview (7.0.8.1) lib/action_view/renderer/renderer.rb:61:in `render_template_to_object'
 - actionview (7.0.8.1) lib/action_view/renderer/renderer.rb:29:in `render_to_object'
 - actionview (7.0.8.1) lib/action_view/rendering.rb:117:in `block in _render_template'
 - actionview (7.0.8.1) lib/action_view/base.rb:270:in `in_rendering_context'
 - actionview (7.0.8.1) lib/action_view/rendering.rb:116:in `_render_template'
 - actionpack (7.0.8.1) lib/action_controller/metal/streaming.rb:216:in `_render_template'
 - actionview (7.0.8.1) lib/action_view/rendering.rb:103:in `render_to_body'
 - actionpack (7.0.8.1) lib/action_controller/metal/rendering.rb:158:in `render_to_body'
 - actionpack (7.0.8.1) lib/action_controller/metal/renderers.rb:141:in `render_to_body'
 - actionpack (7.0.8.1) lib/abstract_controller/rendering.rb:27:in `render'
 - actionpack (7.0.8.1) lib/action_controller/metal/rendering.rb:139:in `render'
 - actionpack (7.0.8.1) lib/action_controller/metal/instrumentation.rb:22:in `block (2 levels) in render'
 - /Users/xxx/.rbenv/versions/3.1.2/lib/ruby/3.1.0/benchmark.rb:311:in `realtime'
 - activesupport (7.0.8.1) lib/active_support/core_ext/benchmark.rb:14:in `ms'
 - actionpack (7.0.8.1) lib/action_controller/metal/instrumentation.rb:22:in `block in render'
 - actionpack (7.0.8.1) lib/action_controller/metal/instrumentation.rb:91:in `cleanup_view_runtime'
 - activerecord (7.0.8.1) lib/active_record/railties/controller_runtime.rb:34:in `cleanup_view_runtime'
 - actionpack (7.0.8.1) lib/action_controller/metal/instrumentation.rb:21:in `render'
 - actionpack (7.0.8.1) lib/action_controller/metal/implicit_render.rb:35:in `default_render'
 - actionpack (7.0.8.1) lib/action_controller/metal/basic_implicit_render.rb:7:in `send_action'
 - actionpack (7.0.8.1) lib/abstract_controller/base.rb:215:in `process_action'
 - scout_apm (5.3.1) lib/scout_apm/instruments/action_controller_rails_3_rails4.rb:103:in `process_action'
 - actionpack (7.0.8.1) lib/action_controller/metal/rendering.rb:165:in `process_action'
 - actionpack (7.0.8.1) lib/abstract_controller/callbacks.rb:234:in `block in process_action'
 - activesupport (7.0.8.1) lib/active_support/callbacks.rb:118:in `block in run_callbacks'
 - actiontext (7.0.8.1) lib/action_text/rendering.rb:20:in `with_renderer'
 - actiontext (7.0.8.1) lib/action_text/engine.rb:69:in `block (4 levels) in <class:Engine>'
 - activesupport (7.0.8.1) lib/active_support/callbacks.rb:127:in `block in run_callbacks'
 - activesupport (7.0.8.1) lib/active_support/callbacks.rb:138:in `run_callbacks'
 - actionpack (7.0.8.1) lib/abstract_controller/callbacks.rb:233:in `process_action'
 - actionpack (7.0.8.1) lib/action_controller/metal/rescue.rb:23:in `process_action'
 - actionpack (7.0.8.1) lib/action_controller/metal/instrumentation.rb:67:in `block in process_action'
 - activesupport (7.0.8.1) lib/active_support/notifications.rb:206:in `block in instrument'
 - activesupport (7.0.8.1) lib/active_support/notifications/instrumenter.rb:24:in `instrument'
 - activesupport (7.0.8.1) lib/active_support/notifications.rb:206:in `instrument'
 - actionpack (7.0.8.1) lib/action_controller/metal/instrumentation.rb:66:in `process_action'
 - actionpack (7.0.8.1) lib/action_controller/metal/params_wrapper.rb:259:in `process_action'
 - activerecord (7.0.8.1) lib/active_record/railties/controller_runtime.rb:27:in `process_action'
 - scout_apm (5.3.1) lib/scout_apm/instruments/action_controller_rails_3_rails4.rb:120:in `process_action'
 - actionpack (7.0.8.1) lib/abstract_controller/base.rb:151:in `process'
 - actionview (7.0.8.1) lib/action_view/rendering.rb:39:in `process'
 - actionpack (7.0.8.1) lib/action_controller/metal.rb:188:in `dispatch'
 - actionpack (7.0.8.1) lib/action_controller/metal.rb:251:in `dispatch'
 - actionpack (7.0.8.1) lib/action_dispatch/routing/route_set.rb:49:in `dispatch'
 - actionpack (7.0.8.1) lib/action_dispatch/routing/route_set.rb:32:in `serve'
 - actionpack (7.0.8.1) lib/action_dispatch/journey/router.rb:50:in `block in serve'
 - actionpack (7.0.8.1) lib/action_dispatch/journey/router.rb:32:in `serve'
 - actionpack (7.0.8.1) lib/action_dispatch/routing/route_set.rb:852:in `call'
 - scout_apm (5.3.1) lib/scout_apm/instruments/rails_router.rb:29:in `call_with_scout_instruments'
 - omniauth (2.0.4) lib/omniauth/strategy.rb:202:in `call!'
 - omniauth (2.0.4) lib/omniauth/strategy.rb:169:in `call'
 - omniauth (2.0.4) lib/omniauth/strategy.rb:202:in `call!'
 - omniauth (2.0.4) lib/omniauth/strategy.rb:169:in `call'
 - rack-attack (6.6.1) lib/rack/attack.rb:127:in `call'
 - scout_apm (5.3.1) lib/scout_apm/instant/middleware.rb:55:in `call'
 - scout_apm (5.3.1) lib/scout_apm/middleware.rb:17:in `call'
 - rack (2.2.9) lib/rack/deflater.rb:44:in `call'
 - warden (1.2.9) lib/warden/manager.rb:36:in `block in call'
 - warden (1.2.9) lib/warden/manager.rb:34:in `call'
 - rack (2.2.9) lib/rack/tempfile_reaper.rb:15:in `call'
 - rack (2.2.9) lib/rack/etag.rb:27:in `call'
 - rack (2.2.9) lib/rack/conditional_get.rb:40:in `call'
 - rack (2.2.9) lib/rack/head.rb:12:in `call'
 - actionpack (7.0.8.1) lib/action_dispatch/http/permissions_policy.rb:38:in `call'
 - actionpack (7.0.8.1) lib/action_dispatch/http/content_security_policy.rb:36:in `call'
 - rack (2.2.9) lib/rack/session/abstract/id.rb:266:in `context'
 - rack (2.2.9) lib/rack/session/abstract/id.rb:260:in `call'
 - actionpack (7.0.8.1) lib/action_dispatch/middleware/cookies.rb:704:in `call'
 - activerecord (7.0.8.1) lib/active_record/migration.rb:638:in `call'
 - actionpack (7.0.8.1) lib/action_dispatch/middleware/callbacks.rb:27:in `block in call'
 - activesupport (7.0.8.1) lib/active_support/callbacks.rb:99:in `run_callbacks'
 - actionpack (7.0.8.1) lib/action_dispatch/middleware/callbacks.rb:26:in `call'
 - actionpack (7.0.8.1) lib/action_dispatch/middleware/executor.rb:14:in `call'
 - actionpack (7.0.8.1) lib/action_dispatch/middleware/actionable_exceptions.rb:17:in `call'
 - better_errors (2.9.1) lib/better_errors/middleware.rb:87:in `protected_app_call'
 - better_errors (2.9.1) lib/better_errors/middleware.rb:82:in `better_errors_call'
 - better_errors (2.9.1) lib/better_errors/middleware.rb:60:in `call'
 - sentry-rails (5.4.2) lib/sentry/rails/rescued_exception_interceptor.rb:12:in `call'
 - actionpack (7.0.8.1) lib/action_dispatch/middleware/debug_exceptions.rb:28:in `call'
 - web-console (4.2.0) lib/web_console/middleware.rb:132:in `call_app'
 - web-console (4.2.0) lib/web_console/middleware.rb:28:in `block in call'
 - web-console (4.2.0) lib/web_console/middleware.rb:17:in `call'
 - sentry-ruby (5.4.2) lib/sentry/rack/capture_exceptions.rb:28:in `block (2 levels) in call'
 - sentry-ruby (5.4.2) lib/sentry/hub.rb:199:in `with_session_tracking'
 - sentry-ruby (5.4.2) lib/sentry-ruby.rb:372:in `with_session_tracking'
 - sentry-ruby (5.4.2) lib/sentry/rack/capture_exceptions.rb:19:in `block in call'
 - sentry-ruby (5.4.2) lib/sentry/hub.rb:59:in `with_scope'
 - sentry-ruby (5.4.2) lib/sentry-ruby.rb:352:in `with_scope'
 - sentry-ruby (5.4.2) lib/sentry/rack/capture_exceptions.rb:18:in `call'
 - actionpack (7.0.8.1) lib/action_dispatch/middleware/show_exceptions.rb:29:in `call'
 - railties (7.0.8.1) lib/rails/rack/logger.rb:40:in `call_app'
 - railties (7.0.8.1) lib/rails/rack/logger.rb:25:in `block in call'
 - activesupport (7.0.8.1) lib/active_support/tagged_logging.rb:99:in `block in tagged'
 - activesupport (7.0.8.1) lib/active_support/tagged_logging.rb:37:in `tagged'
 - activesupport (7.0.8.1) lib/active_support/tagged_logging.rb:99:in `tagged'
 - railties (7.0.8.1) lib/rails/rack/logger.rb:25:in `call'
 - sprockets-rails (3.2.2) lib/sprockets/rails/quiet_assets.rb:13:in `call'
 - actionpack (7.0.8.1) lib/action_dispatch/middleware/remote_ip.rb:93:in `call'
 - request_store (1.5.1) lib/request_store/middleware.rb:19:in `call'
 - actionpack (7.0.8.1) lib/action_dispatch/middleware/request_id.rb:26:in `call'
 - rack (2.2.9) lib/rack/method_override.rb:24:in `call'
 - rack (2.2.9) lib/rack/runtime.rb:22:in `call'
 - activesupport (7.0.8.1) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
 - actionpack (7.0.8.1) lib/action_dispatch/middleware/executor.rb:14:in `call'
 - actionpack (7.0.8.1) lib/action_dispatch/middleware/static.rb:23:in `call'
 - rack (2.2.9) lib/rack/sendfile.rb:110:in `call'
 - actionpack (7.0.8.1) lib/action_dispatch/middleware/host_authorization.rb:138:in `call'
 - utf8-cleaner (1.0.0) lib/utf8-cleaner/middleware.rb:21:in `call'
 - vite_ruby (3.5.0) lib/vite_ruby/dev_server_proxy.rb:22:in `perform_request'
 - rack-proxy (0.7.7) lib/rack/proxy.rb:87:in `call'
 - scout_apm (5.3.1) lib/scout_apm/instruments/middleware_summary.rb:58:in `call'
 - railties (7.0.8.1) lib/rails/engine.rb:530:in `call'
 - puma (5.6.8) lib/puma/configuration.rb:252:in `call'
 - puma (5.6.8) lib/puma/request.rb:77:in `block in handle_request'
 - puma (5.6.8) lib/puma/thread_pool.rb:340:in `with_force_shutdown'
 - puma (5.6.8) lib/puma/request.rb:76:in `handle_request'
 - puma (5.6.8) lib/puma/server.rb:443:in `process_client'
 - puma (5.6.8) lib/puma/thread_pool.rb:147:in `block in spawn_thread'

Additional Information

Ruby version: 3.1.2
Rails version: 7.0.8.1
Jbuilder version: 2.11.2

Could you please help investigate this issue? Any guidance on how to properly declare and use constants in Jbuilder files, or adjustments needed to avoid such errors, would be greatly appreciated.

@vinibispo
Copy link

@Shao-Ting-Fong, according to my tests, that isn't possible because you would be doing a dynamic constant assignment.
That happens because jbuilder/action_view "converts" your partial/view to a ruby method, and as you know, it's not possible to declare a constant inside a ruby method; see https://www.rubyguides.com/2017/07/ruby-constants/

@vinibispo
Copy link

Let me know if I misexplained something @rafaelfranca

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants