Skip to content

Instantly share code, notes, and snippets.

@paulcsmith
Created January 19, 2019 00:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulcsmith/70eac0436a32fa2abe1c87a03f2d257f to your computer and use it in GitHub Desktop.
Save paulcsmith/70eac0436a32fa2abe1c87a03f2d257f to your computer and use it in GitHub Desktop.

From Rails to Lucky (Guide)

#lucky

Controllers and routing

In Rails you have controllers, and controllers have actions. In Lucky, we just have “Actions.” Another different is that Lucky handles routing in the action instead of in a different routes file

What’s different?

  • Instead of a controller class with methods for actions, Lucky just has “actions” that are single classes. E.g. (UsersController#index in Rails is Users::Index in Lucky)
  • Lucky defines routes in the action, not in a separate routes file
  • Lucky can infer RESTful routes and namespaces for you
# config/routes.rb
resources :users, only: :index

# app/controllers/users_controller.rb
class UsersController < ApplicationController
  def index
  end
end

Serving and parsing content

What’s different?

Rendering

What’s different?

  • Lucky requires you to tell it what to render.
  • Lucky requires the render to be returned at the end of the action
  • Instance variables are rarely used in Lucky actions, and are not automatically passed to the view

For HTML:

# src/actions/users/index.cr
class Users::Index < BrowserAction
  route do
    render IndexPage, name: "Paul"
  end
end

Nested resources

Custom routes

Before and after filters

Models

Validations

Rails:

class User < ApplicationRecord
  validates :name, presence: true
  validate :custom_validate, if: :custom_method?
end

Lucky:

class UserForm < User::BaseForm
  def prepare
    custom_validation if custom_method?
  end
end

In-depth guide: Screencasts:

Form and JSON params

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment