Actions
Actions are ultimately what respond to the request from the client. They are defined as methods on your controller class.
Simple
# app/controllers/users_controller.rb
class UsersController
include Mack::Controller
def show
@user = User.first(params[:id])
end
end
# config/routes.rb
Mack::Routes.build do |r|
r.resources :users
end
# app/views/users/show.html.erb
Hello '<%= @user.display_name %>'
When a request comes in with the url '/users/1' it gets routed to the UsersController and the show action. A User is looked up from the database, let's say, based on the id parameter from the url. Once the user is found an instance variable, @user is set, and the action returns. Mack then renders the app/views/users/show.html.erb template, which has access to the instance variables set by the action in the controller.
No Action Defined
An action method doesn't necessarily have to be defined for a request to still be handled by the view template for our action:
# app/controllers/users_controller.rb class UsersController end # config/routes.rb Mack::Routes.build do |r| r.say_hi '/users/say_hi', :controller => :users, :actions => :say_hi end # app/views/users/say_hi.html.erbHello World!
When the request comes in for '/users/say_hi' Mack will see that there is no say_hi action in the UsersController so it will attempt to find the say_hi.html.erb template on disk. Finding that it will render and return that template.
No Action and No Template
If a request comes in and there is no action and template defined Mack will attempt to find a static page in the public directory that matches the uri path, if one is found, then that static page is returned, otherwise a Mack::Errors::ResourceNotFound error is raised.
NOTE: For an action to be able to respond to a request it has to be public. If the action is any other state, say protected or private, it will not be able to respond to the request.