Note that there are some explanatory texts on larger screens.

plurals
  1. PODetermine if action is on member or on collection
    primarykey
    data
    text
    <p>I have been trying to find a clean way to determine wether an action (in a rails controller) applies on members or on collection. </p> <p>For example when you declare in routes.rb</p> <pre><code>resources :projects </code></pre> <p>You get the following methods defined "on collections":</p> <ul> <li>index</li> <li>create</li> <li>new</li> </ul> <p>and the following are defined on "on members":</p> <ul> <li>update</li> <li>show</li> <li>delete</li> </ul> <p>I am trying to find a way to leverage this great pattern in views for example:</p> <pre><code>&lt;% if @controller.action.applies_on_members? %&gt; &lt;%= link_to :destroy, :method =&gt; "delete", :confirm =&gt; "RU Sure" %&gt; &lt;%= link_to :show, "show" %&gt; &lt;% end %&gt; </code></pre> <p>Similarly this could be useful in before_filters</p> <pre><code>before_filter :find_project, :only_on_members =&gt; true </code></pre> <p><em>Currently</em> I have to do the following:</p> <pre><code>before_filter :find_project, :except =&gt; [:new, :create, :index, :export_all, :destroy_all, :archive_all] </code></pre> <p>This is quite annoying and my idea is that all such actions share a common behavior: they are defined on <em>collections</em>. </p> <p>Does anyone have an idea of how to achieve this in clean way ? </p> <p><strong>NB: The reason I ask this question is because I am looking for something scalable in terms of development, such that the behavior of before_filters or some partials is automatically inherited by new custom actions.</strong></p> <p>Who never had to write down something like</p> <pre><code>&lt;% unless ["new", "index", "some_other_action1", "some_other_action2", "some_other_action3", "some_other_action4"].include? @controller.action_name %&gt; &lt;%= link_to "Destroy", :project, :method =&gt; :delete %&gt; &lt;% end %&gt; </code></pre>
    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.
 

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