Tutorial
👋 User, welcome to the Lato tutorial!
These pages were generated by downloading the rendered HTML templates from Lato's dummy app.
Some features might not be available. To use the full documentation, you need to run the gem locally using the following commands:
$ git clone https://github.com/lato-org/lato $ cd lato $ bundle $ rails db:migrate $ rails db:seed $ foreman start -f Procfile.dev
Getting Started
To create custom presentation pages for your project, you can modify the ApplicationController
by extending the Lato::ApplicationController
class and specifying the use of Lato's layout:
class ApplicationController < Lato::ApplicationController layout 'lato/application' def index; end end
Source code: Example
Source code: Lato::ApplicationController
How to Hide the Sidebar
To hide the sidebar, you can use one of the methods provided by the Lato::Layoutable
concern (already included via Lato::ApplicationController
):
class ApplicationController < Lato::ApplicationController layout 'lato/application' def index hide_sidebar end end
To apply this rule to all controller actions, you can quickly use before_action
:
class ApplicationController < Lato::ApplicationController layout 'lato/application' before_action :hide_sidebar def index; end def other_action; end end
How to Use Custom CSS and Customize Bootstrap
To customize Lato's CSS, you can modify your main application's application.scss
file:
@import url('https://fonts.googleapis.com/css2?family=Lato:ital,wght@0,100;0,300;0,400;0,700;0,900;1,100;1,300;1,400;1,700;1,900&display=swap'); // Override bootstrap variables $font-family-base: 'Lato' !default; $primary: #03256C; // Import Lato CSS @import 'lato/application'; // Add custom CSS pre { border-radius: 15px; }
Source code: Example
Source code: Bootstrap SCSS Variables
How to Use Custom Stimulus JS Controllers
Stimulus JS is a dependency of Lato and can be used normally without specific integrations.
Below is an example of using a custom controller hello_controller.js
and a Lato controller lato_hello_controller.js
:
Notes
All controllers handled by Lato are named, by definition, lato_name_controller.js
.
Example: lato_hello_controller.js
is used with the attribute data-controller="lato-hello"
.
How to Protect a Page
To authenticate a page and prevent public access by unauthenticated users, you can use one of the methods provided by the Lato::Sessionable
concern (already included via Lato::ApplicationController
):
class TutorialController < ApplicationController def index return unless authenticate_session end end
To apply the rule to all actions in the controller, you can simply use before_action
:
class TutorialController < ApplicationController before_action :authenticate_session def index; end end
Notes
Just like authenticate_session
, you can also use its opposite not_authenticate_session
to hide a page from authenticated users.
Example:
class AuthenticationController < ApplicationController before_action :not_authenticate_session, only: %i[signin] before_action :authenticate_session, only: %i[signout] def signin; end def signout; end end
Limiting the Number of Requests
To limit the number of requests to a page, you can use the limit_request
method:
class TutorialController < ApplicationController before_action :authenticate_session before_action :limit_request, only: %i[index] def index; end end
The limit_request
method accepts two parameters: limit
(default: 10) and time_window
(default: 10.minutes).
These parameters define the maximum number of requests a user can make within a given time window.
Source code: Lato::Sessionable
Accessing the Logged-in User
The user currently logged into Lato is always accessible in all controllers and views through the @session
instance of the Lato::Session
model:
class TutorialController < ApplicationController # ... def index @user_id = @session.user_id @user = @session.user.first_name end # ... end
Notes
Using @session.user_id
instead of @session.user.id
avoids triggering a query on the users table, improving performance!
@session.valid?
lets you check whether the user is logged in or not.