【问题标题】:Using helpers (or render partials) from custom RedCloth tags使用来自自定义 RedCloth 标签的助手(或渲染部分)
【发布时间】: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


    【解决方案1】:

    好的,所以我自己找到了一个可以接受的解决方案。所以我会成为一个好孩子并分享。

    我将使用一个呈现部分的类而不是助手,所以问题应该变成:“从自定义 RedCloth 标签调用部分”。

    (我没有评论代码,它应该是非常自我记录的)

    app/helpers/schedule_tag.rb:

    module ScheduleTag
      def schedule(options)
        Schedule.new.render Date.today.monday
      end
    end
    

    app/helpers/schedule.rb:

    class Schedule < ActionView::Base
      include ActionView::Helpers::TagHelper
      include Rails.application.routes.url_helpers
    
      def render week
        self.view_paths = "app/views"
        super :partial => "schedule/schedule",
              :locals => { :week => week,
                           :schedule => ScheduledClass.get_schedule(week) }
      end
    end
    

    app/view/schedule/_schedule.html.erb:

    <table id="schedule">
      <tbody>
        <tr>
          <td>Hoop-dee-doo, it works!</td>
          <%= content_tag :td do %>
            <%= link_to "so do tag & route helpers", happy_monkey_path(:happy => "very") %>
          <% end %>
        </tr>
      </tbody>
    </table>
    

    顺便说一句,如果你尝试这个,你可能会偶然发现 RedCloth 转义你的选项字符串的问题。只需将选项包含在== 中,即可将选项逐字传递。例如schedule. =={"weeks":["this","next"]}==。这可能有点危险,所以一定要检查一下是否肮脏。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-07
      • 2019-11-07
      • 1970-01-01
      • 2017-04-19
      • 2012-01-31
      • 2017-04-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多