First of all, the template for working with collections and their records is part of REST , which is one of the pillars of Rails. What you defined is that REST calls collections and records . Theoretically, you can apply the most common HTTB verbs in a collection or in a record inside this collection, where: GET ; retrieve, put ; replace POST ; create and DELETE ; to destroy. Although it is a little incomprehensible what POST will do in one record.
In practice and in Rails, however, you usually use the ones you have identified:
- Collections
- Create Post ( POST )
- View Records ( GET )
- entrance
- Update Record ( PUT )
- Show Record ( GET )
- Delete record ( DELETE )
These are RESTful routes added by Rails. However, as you know, Rails also includes two other actions for implementing a custom page that can provide the necessary data to these endpoints. ( new , change )
I rarely see people approach these definitions. Since I see this, there are at least two ways to solve this problem if you really want to solve it (see the bottom of the post):
You can defuse "_ normalize_callback_options" to get the exact syntax you suggested. This is relatively straightforward, but if the upstream does not change anything in the structure, you need to change the patch, so it is not very stable. Therefore, I would not recommend this approach, no matter how tasteful the syntactic sugar looks. Of course, you can also try to make this upstream, but it can hardly be accepted for the reasons mentioned below. :-)
Instead, I would put your definitions in an initializer: config/initializer/rest.rb
ENTRY_ACTIONS = [:edit, :new, :show, :delete, :update] COLLECTION_ACTIONS = [:create, :index]
Then use them in your filters, for example:
before_filter :find_project, only: ENTRY_ACTIONS
To access your views, add the entry_action? method entry_action? and collection_action? to your application helper:
module ApplicationHelper def entry_action? ENTRY_ACTIONS.include?(controller.action_name.to_sym) end def collection_action? COLLECTION_ACTIONS.include?(controller.action_name.to_sym) end end
Then in your views:
<% if entry_action? %> # code <% end %>
I would not extract this for constants myself, but just wrote the array directly, as it is much more clear, in my opinion, for reading when I return later. In addition, most people have had to think twice each time they come across these methods, which are far from optimal.