Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby on Rails Scoping code block to remove the need for arguments?
    text
    copied!<p>I come from PHP and CakePHP background and I'm pretty new to ruby and rails.</p> <p>I've been creating a helper that could help me with creating some HTML elements that could make me easy to reuse across the web app that I am creating.</p> <p>Here's how the snippet of my helper looks like</p> <pre><code>module VehicleHelper def mileage(vehicle) render partial: "vehicles/shared/mileage", { locals: vehicle } end def manufacturer(vehicle) render partial: "vehicles/shared/manufacturer", { locals: vehicle } end #and etc... end </code></pre> <p>And I would use it this way in haml.</p> <pre><code>%h1= @vehicle.name = mileage @vehicle = manufacturer @vehicle -# and etc… </code></pre> <p>I want to be able to scope it, so that I don't need to give <code>@vehicle</code> as an argument for every function. Like</p> <pre><code>%h1= @vehicle.name - vehicle_block_for @vehicle do = mileage = manufacturer -# and etc… </code></pre> <p>How do I achieve it? Is that a right pattern that I should use?</p> <p><strong>Edit</strong> : I have thought of using partials straight in the view. However, the problem is that I not only have <code>Vehicle</code> class, I also have <code>Car</code> and <code>Bike</code> classes which are sub-classes of <code>Vehicle</code>, following the STI pattern. </p> <p>Which means I will have to pass locals all the time, in which case <code>= render partial: 'mileage', { locals: vehicle}</code> or what not becomes configuration codes. And let's say I've renamed the file or moved it somewhere, then I'd have to go and modify all these codes.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload