カスタム RedCloth タグからビュー ヘルパーを呼び出そうとしていますが、まったく運がありません。私が見つけることができるすべての例では、カスタム タグ メソッドを作成し、他のメソッドをまったく呼び出さずにリテラル文字列を返します。
毎週のスケジュールを出力するカスタム RedCloth タグを作成しようとしています。このコードは、アプリケーションの他の領域でも使用されるため、既にヘルパーに記述されています。
だから、私の問題が要約される(大幅に削減された)コードは次のとおりです。
アプリ/ビュー/ページ/show.html.erb:
<%= raw redcloth @page.content %
アプリ/ヘルパー/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
実行されると、Rails は次のように主張します。
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'
ヘルパーの使用からクラスの使用に変更することで少し進歩することができますが、標準のヘルパーを使用すると、そのクラスにはまだ問題があります。
/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
そしてレールは言う:
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'
それで、私は何をすべきですか?私は完全に明らかな何かを見逃しており、恥をかいて頭を埋める必要がありますか、それとも、好きな場所からヘルパーを呼び出すことは単に不可能ですか? 自分の ScheduleHelper モジュールのすべてのコードを複製するカスタム タグを取得する必要がありますか?
どんな助けでも大歓迎です。