module Orion::DSL::Resources
Overview
A common way in Orion to route is to do so against a known resource. This method will create a series of routes targeted at a specific controller.
The following is an example controller definition and the matching resources definition.
class PostsController
include Orion::ControllerHelper
include ResponseHelpers
def index
@posts = Post.all
render :index
end
def new
@post = Post.new
render :new
end
def create
post = Post.create(request)
redirect to: post_path post_id: post.id
end
def show
@post = Post.find(request.path_params["post_id"])
end
def edit
@post = Post.find(request.path_params["post_id"])
render :edit
end
def update
post = Post.find(request.path_params["post_id"])
HTTP::FormData.parse(request) do |part|
post.attributes[part.name] = part.body.gets_to_end
end
redirect to: post_path post_id: post.id
end
def delete
post = Post.find(request.path_params["post_id"])
post.delete
redirect to: posts_path
end
end
resources :posts
Including/Excluding Actions
By default, the actions index
, new
, create
, show
, edit
, update
, delete
are included. You may include or exclude explicitly by using the only
and except
params.
NOTE: The index action is not added for singular resources.
resources :posts, except: [:edit, :update]
resources :users, only: [:new, :create, :show]
Nested Resources and Routes
You can add nested resources and member routes by providing a block to the
resources
definition.
resources :posts do
post "feature", action: feature
resources :likes
resources :comments
end
Singular Resources
In addition to using the collection of resources
method, You can also add
singular resources which do not provide a id_param
or index
action.
resource :profile
Customizing ID
You can customize the ID path parameter by passing the id_param
parameter.
resources :posts, id_param: :article_id
Constraining the ID
You can set constraints on the ID parameter by passing the id_constraint
parameter.
See Orion::DSL::Constraints
for more details on constraints.
resources :posts, id_constraint: /^\d{4}$/
Defined in:
Macro Summary
- resource(name, *, controller = nil, only = nil, except = nil, format = nil, accept = nil, content_type = nil, type = nil)
- resources(name, *, controller = nil, only = nil, except = nil, id_constraint = nil, format = nil, accept = nil, id_param = nil, content_type = nil, type = nil)