Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If only there were some kind of view inheritance... So that one can subclass controller without need to supply all its views. Good thing is that there is this <a href="http://dev.rubyonrails.org/ticket/7076" rel="nofollow noreferrer">patch</a>. Bad thing is that it can't make it to the core for quite a while.</p> <p>Having applied it to my rails 2.2, I managed to have the following answer to the original question.</p> <p><strong>Subclassing controller</strong> </p> <p>ProductController has been blessed with the twins:</p> <pre><code>class Products::AdminController &lt; ProductsController layout 'admin' before_filter :authenticate end </code></pre> <p>and</p> <pre><code>class Products::StoreController &lt; ProductsController layout 'store' before_filter :find_cart end </code></pre> <p>This itself looks quite nice since each of them as well carries its own initialization part.</p> <p><strong>Changing routes</strong></p> <pre><code> map.resources :products, :controller =&gt; 'products/admin', :path_prefix =&gt; 'admin', :name_prefix =&gt; 'admin_' map.resources :products, :controller =&gt; 'products/store', :path_prefix =&gt; 'store', :only =&gt; [:show, :index], :name_prefix =&gt; 'store_' </code></pre> <p>Not an easy route, defo. But, hey, after this point everything just works (assuming you fixed path helpers) with ProductController views and partials.</p> <p><strong>Shared views changes</strong></p> <p>Each subclass controller has its own version of index.html.erb. Everything else is shared in a base class.</p> <p>Speaking about path helpers in shared templates. What used to be</p> <pre><code>&lt;% form_for @product ... %&gt; </code></pre> <p>becomes</p> <pre><code>&lt;% form_for [controller_name, @product] ... %&gt; </code></pre> <p>and thins like</p> <pre><code>&lt;%= link_to products_path %&gt; </code></pre> <p>turn into</p> <pre><code>&lt;%= link_to send("#{controller_name}_products_path") %&gt; </code></pre> <p>I don't know if it is all worth it, but that is a way. Anyone knows why if there are plans to include this patch in rails soon?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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