如何在Rails 3中完成这项工作?

时间:2011-09-13 08:55:45

标签: ruby ruby-on-rails-3

我正在更新一个Rails项目,发现一段代码在Rails 3和Ruby 1.9.2中没有用(它在Rails 2.3和Ruby 1.8.7中都有用),反正看起来很乱!

  def speech_box(title, options = {}, &block)
    block_to_partial 'layouts/speech_box', options.merge(:title => title), &block
  end

  def block_to_partial(partial_name, options = {}, &block)
    options.merge!(:body => capture(&block))
    concat(render(:partial => partial_name, :locals => options), block.binding)
  end

我打电话时收到以下错误:

  <% speech_box("hello") do %>
    <p>lorem ipsum</p>
  <% end %>
  

错误的参数数量(2对1)

为什么这在Rails 3中不起作用?

根据下面的要求,这里是跟踪:

actionpack (3.1.0) lib/action_view/helpers/text_helper.rb:51:in `concat'
app/helpers/application_helper.rb:277:in `block_to_partial'
app/helpers/application_helper.rb:272:in `speech_box'
app/views/talks/new.html.erb:46:in `_app_views_talks_new_html_erb__3484685084088947386_2193902320'
actionpack (3.1.0) lib/action_view/template.rb:144:in `block in render' activesupport (3.1.0)
lib/active_support/notifications.rb:55:in `instrument' actionpack (3.1.0)
lib/action_view/template.rb:142:in `render' actionpack (3.1.0)
lib/action_view/renderer/template_renderer.rb:40:in `block (2 levels) in render_template' actionpack (3.1.0)
lib/action_view/renderer/abstract_renderer.rb:33:in `block in instrument' activesupport (3.1.0)
lib/active_support/notifications.rb:53:in `block in instrument' activesupport (3.1.0)
lib/active_support/notifications/instrumenter.rb:21:in `instrument' activesupport (3.1.0)
lib/active_support/notifications.rb:53:in `instrument' actionpack (3.1.0)
lib/action_view/renderer/abstract_renderer.rb:33:in `instrument' actionpack (3.1.0)
lib/action_view/renderer/template_renderer.rb:39:in `block in render_template' actionpack (3.1.0)
lib/action_view/renderer/template_renderer.rb:47:in `render_with_layout' actionpack (3.1.0)
lib/action_view/renderer/template_renderer.rb:38:in `render_template' actionpack (3.1.0)
lib/action_view/renderer/template_renderer.rb:12:in `block in render' actionpack (3.1.0)
lib/action_view/renderer/abstract_renderer.rb:22:in `wrap_formats' actionpack (3.1.0)
lib/action_view/renderer/template_renderer.rb:9:in `render' actionpack (3.1.0)
lib/action_view/renderer/renderer.rb:36:in `render_template' actionpack (3.1.0)
lib/action_view/renderer/renderer.rb:17:in `render' actionpack (3.1.0)
lib/abstract_controller/rendering.rb:120:in `_render_template' actionpack (3.1.0)
lib/action_controller/metal/streaming.rb:250:in `_render_template' actionpack (3.1.0)
lib/abstract_controller/rendering.rb:114:in `render_to_body' actionpack (3.1.0)
lib/action_controller/metal/renderers.rb:30:in `render_to_body' actionpack (3.1.0)
lib/action_controller/metal/compatibility.rb:43:in `render_to_body' actionpack (3.1.0)
lib/abstract_controller/rendering.rb:99:in `render' actionpack (3.1.0)
lib/action_controller/metal/rendering.rb:16:in `render' actionpack (3.1.0)
lib/action_controller/metal/instrumentation.rb:40:in `block (2 levels) in render' activesupport (3.1.0)
lib/active_support/core_ext/benchmark.rb:5:in `block in ms'
.............

1 个答案:

答案 0 :(得分:4)

concat消失了。只需删除concat,并确保使用speech_box而不是<%=来致电<%

def block_to_partial(partial_name, options = {}, &block)
  options.merge!(:body => capture(&block))
  render(:partial => partial_name, :locals => options)
end

<%= speech_box("hello") do %>
  <p>lorem ipsum</p>
<% end %>