开发者

How to make this work in Rails 3?

I am updating a Rails project and found a piece of code that doesn't work in Rails 3 and Ruby 1.9.2 (it did in Rails 2.3 and Ruby 1.8.7) and looks quite messy anyway!

  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

I am getting the following error when I call:

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

wrong number of arguments (2 for 1)

How come this doesn't work in Rails 3?

As requested below, here is the trace:

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' actio开发者_运维百科npack (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'
.............


concat is gone. Simply remove concat and make sure to call speech_box using <%= instead of <%.

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 %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜