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' 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'
.............
Go to your browser and open http://localhost:3000, you will see a basic Rails app running. You can also use the alias "s" to start the server: bin/rails s . The server can be run on a different port using the -p option. The default development environment can be changed using -e .
To generate a fully working scaffold for a new object, including model, controller, views, assets, and tests, use the rails g scaffold command. Then you can run rake db:migrate to set up the database table. Then you can visit http://localhost:3000/widgets and you'll see a fully functional CRUD scaffold.
To generate a controller and optionally its actions, do the following: Press Ctrl twice and start typing rails g controller. Select rails g controller and press Enter . In the invoked Add New Controller dialog, specify the controller name.
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 %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With