rails block concat

helper method to partial

concat can be useful for rendering a block to a partial from a helper:

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

This would be particularly useful if you had some partial to help you out with rounded corners, for example. So, in your helper:

 def rounded_corners &block
   block_to_partial("shared/rounded_corners", {}, &block)
 end

In your view you could have something like:

 <% rounded_corners do -%>
      This text is surrounded by rounded corners
 <% end -%>

You would have to create some partial in

  app/views/shared/rounded_corners.html.erb

And it would look something like:

  <div class='c1'>
    <div class=c2>
      .
      .
      .
      <%= body -%>
    </div>
  </div>

 

你可能感兴趣的:(html,Rails)