Ruby on rails 使用自定义红布标记中的辅助对象(或渲染部分)
我试图从一个定制的红布标签中调用一个视图助手,但我一点运气都没有。我能找到的所有示例都使用自定义标记方法创建并返回文本字符串,而不调用任何其他方法 我试图创建一个自定义的红布标签来输出一个每周计划;已在助手中编写的代码,因为它也用于应用程序的其他区域 下面是我的问题归结为的(大量减少的)代码: app/views/page/show.html.erb:Ruby on rails 使用自定义红布标记中的辅助对象(或渲染部分),ruby-on-rails,ruby-on-rails-3,partials,custom-tag,redcloth,Ruby On Rails,Ruby On Rails 3,Partials,Custom Tag,Redcloth,我试图从一个定制的红布标签中调用一个视图助手,但我一点运气都没有。我能找到的所有示例都使用自定义标记方法创建并返回文本字符串,而不调用任何其他方法 我试图创建一个自定义的红布标签来输出一个每周计划;已在助手中编写的代码,因为它也用于应用程序的其他区域 下面是我的问题归结为的(大量减少的)代码: app/views/page/show.html.erb: <%= raw redcloth @page.content % <table id="schedule"> <t
<%= raw redcloth @page.content %
<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>
app/helpers/schedule_tag.rb:
module ScheduleTag
include ScheduleHelper
def schedule(options)
build_schedule
end
end
module ScheduleTag
def schedule(options)
Schedule.new.render Date.today.monday
end
end
app/helpers/schedule_helper.rb:
module PagesHelper
def redcloth(content)
r = RedCloth.new content
r.extend ScheduleTag
r.to_html
end
end
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'
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'
页面中的命名错误#显示
显示/blah/blah/blah/app/views/pages/show.html.erb,其中第1行出现:
ScheduleBuilder:类的未定义方法“output\u buffer=”
提取的源(第1行附近):
1:
Rails.root:/blah/blah/blah
应用程序跟踪|框架跟踪|完整跟踪
app/helpers/schedule\u builder.rb:5:in'build\u schedule'
app/helpers/schedule_tag.rb:42:in'schedule'
app/helpers/pages\u helper.rb:22:in'redcloth'
app/views/pages/show.html.erb:1:在“app\u views\u pages\u show\u html\u erb\u 756530284\u 81708830\u 535015588”中
app/controllers/pages\u controller.rb:23:in'show'
那我该怎么办?我是否错过了一些显而易见的东西,应该羞愧地掩埋自己的脑袋,还是根本不可能从任何人喜欢的地方打电话给助手?我是否需要将其吸收并获取自定义标记以复制ScheduleHelper模块中的所有代码
任何帮助都将不胜感激。好的,所以我自己找到了一个可以接受的解决方案。所以我会成为一个好孩子和大家分享 我将使用一个呈现分部的类,而不是一个助手,因此问题应该真正变成:“从自定义RedCloth标记调用分部” (我没有对代码进行注释,它应该是非常自我记录的) app/helpers/schedule_tag.rb:
module ScheduleTag
include ScheduleHelper
def schedule(options)
build_schedule
end
end
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
class Schedule“时间表/时间表”,
:locals=>{:week=>week,
:schedule=>ScheduledClass.get_schedule(week)}
结束
结束
app/view/schedule/_schedule.html.erb:
<%= raw redcloth @page.content %
<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>
Hoop dee doo,它起作用了!
“非常”)%>
顺便说一句,如果您尝试此操作,您可能会遇到红布从选项字符串中溢出的问题。只要将选项括在==
中,就可以让它按字面意思传递选项。例如时间表=={“周”:[“本次”、“下一次”]}=
。这可能有点危险,所以一定要检查是否有恶意