gon
gon copied to clipboard
View helpers not defined in extended Rabl templates
Hi,
I've run into an issue that seems to be a "continuation" of https://github.com/gazay/gon/issues/26, which in turn seems related to https://github.com/nesquena/rabl/issues/19 and https://github.com/nesquena/rabl/issues/147
Context: a "topic" has many "exercises". I'm using gon to bootstrap the topic data (including all related exercise instances).
Exercise Rabl template ("exercises/show"):
object @exercise
# probably not relevant
# attributes :id, :topic_id, :instructions, :avg_score
node(:last_attempt) { |exercise| time_ago_in_words(exercise.last_attempt) + " ago" }
# probably not relevant
# child :exercise_items => :questions do
# extends "exercise_items/_base"
# end
Topic Rable template ("topics/show"):
object @topic
# probably not relevant
# extends "topics/base"
child :exercises do
extends "exercises/show"
end
Relevant controller action:
def show
gon.topic = Rabl.render(@topic, "topics/show", format: :json)
redirect_to_web_app
end
Accessing the controller action raises the following error:
undefined method `time_ago_in_words' for #<Rabl::Engine:0x00000004ff08d0>
If I access the API endpoint, Rabl renders the JSON data correctly. In addition, if I comment the gon.topic
line above, the error is no longer raised.
A work around to get this working is to manually include the helper in the controller with
require 'action_view'
include ActionView::Helpers::DateHelper
It seems the issue is related to using a sub-view ("exercises/show") in the "topics/show" Rabl template that is given to gon within the controller.
To be noted: using view helpers within the rendered view provided to gon works as expected. I.e. the following code (in a controller) does not raise an exception:
gon.exercise = Rabl.render(@exercise, "exercises/show", format: :json)
Once again, the issue seems to be present only if a view helper is used in a sub-view (i.e. "exercises/show") that Rabl extends from the "main" view (i.e. "topics/show") provided to gon.
I'm also getting this error and its also related to using sub-views - api/v1/topics/index
- however when I try including the helper in the controller I'm still getting the same error.