You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
Declare a constant at the beginning of a Jbuilder file.
Use this Jbuilder file to render complex JSON structures involving partials.
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 fileAPPLE="123".freeze# JSON structure in Jbuilderjson.seriesDatadojson.array!@indicatorsdo |indicator|
partial_path=File.join(controller_path,indicator.class.name.underscore.pluralize.delete_prefix('indicator/'))json.datadojson.partial!File.join(partial_path,'data_series'),indicator: indicatorendjson.partial!File.join(File.dirname(partial_path),'info'),indicator: indicatorendend
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.
The text was updated successfully, but these errors were encountered:
@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/
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 namedPARTIAL_ROUTER
for routing partial paths, which caused an error. Surprisingly, even a simple string constant likeAPPLE = "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
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
Error Message
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.
The text was updated successfully, but these errors were encountered: