Skip to content

Instantly share code, notes, and snippets.

@faustinoaq
Last active December 2, 2017 06:11
Show Gist options
  • Save faustinoaq/41a9d213d659c8bf6b45cae56ef6542a to your computer and use it in GitHub Desktop.
Save faustinoaq/41a9d213d659c8bf6b45cae56ef6542a to your computer and use it in GitHub Desktop.
Build routes for Amber automatically
def build_url
case verb
when "get"
case action_name
when :index then "/"
when :show then "/:#{param}"
when :new then "/new"
when :edit then "/:#{param}/edit"
end
when "post"
case action_name
when :create then "/"
end
when "patch", "put"
case action_name
when :update then "/:#{param}"
end
when "delete"
case action_name
when :destroy then "/:#{param}"
end
end
end
Amber::Server.configure do |app|
pipeline :web do
plug Amber::Pipe::Error.new
plug Amber::Pipe::Logger.new
plug Amber::Pipe::Session.new
plug Amber::Pipe::Flash.new
plug Amber::Pipe::CSRF.new
end
routes :web do
# <%= verb => <%= class_name %>Controller, :<%= action_name %>
# builds to /<%= class_name %>/<%= action_name %>
get UserController, :login # => /user/login
get UserController, :logout # => /user/logout
post UserController, :register # => /user/register
# verb <%= class_name %>Controller, :<%= action_name %>, with: :<%= param =>
# builds to /<%= class_name.pluralize %>/<%= build_url %>
get PostController, :index # => /posts
get PostController, :show, with: :id # => /posts/:id
get PostController, :new, with: :id # => /posts/new
post PostController, :create # => /posts
get PostController, :edit, with: :id # => /posts/:id/edit
put PostController, :update, with: :id # => /posts/:id
delete PostController, :destroy, with: :id # => /posts/:id
# Already exist, Yeah!
resources PostController, "/posts"
# But we can try to implement something simpler like:
resources PostController
# Using: something like:
# resources <%= class_name %>Controller, <%= class_name.plurilize %>
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment