I'm trying to call a view helper from a custom RedCloth tag and am having absolutely no luck at all. All of the examples I can find have the custom tag method create and return a literal string without calling any other method at all.
I am trying to create a custom RedCloth tag to output a weekly schedule; the code for which has already been written in a helper as it is also used in other areas of the application.
So here's the (massively reduced) code that my problem boils down to:
app/views/page/show.html.erb:
<%= raw redcloth @page.content %
app/helpers/pages_helper.rb:
module PagesHelper
def redcloth(content)
r = RedCloth.new content
r.extend ScheduleTag
r.to_html
end
end
app/helpers/schedule_tag.rb:
module ScheduleTag
include ScheduleHelper
def schedule(options)
build_schedule
end
end
app/helpers/schedule_helper.rb:
module ScheduleHelper
def build_schedule
content_tag :div do
content_tag :span,
"Why won't my helper just work by magic like the rest of " \
"rails seems to?"
end
end
end
When executed, rails claims the following:
TypeError in Pages#show
Showing /blah/blah/blah/app/views/pages/show.html.erb where line #1 raised:
can't convert Symbol into Integer
Extracted source (around line #1):
1: <%= raw redcloth @page.content %>
Rails.root: /blah/blah/blah
Application Trace | Framework Trace | Full Trace
app/helpers/schedule_helper.rb:3:in `build_schedule'
app/helpers/schedule_tag.rb:42:in `schedule'
app/helpers/pages_helper.rb:22:in `redcloth'
app/views/pages/show.html.erb:1:in _app_views_pages_show_html_erb___756530284_81829440__535015588'
app/controllers/pages_controller.rb:23:in `show'
I can make a little bit of progress by changing from using a helper to a class, but that class is still having issues using the standard helpers:
/app/helpers/schedule_builder.rb:
class ScheduleBuilder
extend ActionView::Helpers::TagHelper
def self.build_schedule
content_tag :div do
content_tag :span,
"Why won't my helper just work by magic like the rest of " \
"rails seems to?"
end
end
end
And rails says:
NoMethodError in Pages#show
Showing /blah/blah/blah/app/views/pages/show.html.erb where line #1 raised:
undefined method `output_buffer=' for ScheduleBuilder:Class
Extracted source (around line #1):
1: <%= raw redcloth @page.content %>
Rails.root: /blah/blah/blah
Application Trace | Framework Trace | Full Trace
app/helpers/schedule_builder.rb:5:in `build_schedule'
app/helpers/schedule_tag.rb:42:in `schedule'
app/helpers/pages_helper.rb:22:in `redcloth'
app/views/pages/show.html.erb:1:in `_app_views_pages_show_html_erb___756530284_81708830__535015588'
app/controllers/pages_controller.rb:23:in `show'
So what am I to do? Am I missing something completely obvious and ought to bury my head in shame, or is it simply not possible to call helpers from anywhere one fancies? Do I need to just suck it up and get the custom tag to duplicate all of the code in my ScheduleHelper module?
Any help would be greatly appreciated.