【发布时间】:2011-12-07 05:52:08
【问题描述】:
我正在尝试从自定义 RedCloth 标记调用视图助手,但我完全没有运气。我能找到的所有示例都有自定义标记方法创建并返回一个文字字符串,而根本不调用任何其他方法。
我正在尝试创建一个自定义 RedCloth 标签来输出每周计划;已经在帮助程序中编写的代码,因为它也用于应用程序的其他区域。
所以这是我的问题归结为的(大量减少的)代码:
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
执行时,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
Rails 说:
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 模块中的所有代码吗?
任何帮助将不胜感激。
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 partials custom-tag redcloth