The author of this article is a beginner just starting to learn programming. I would appreciate it if you could point out any mistakes.
This article is a personal memo of what I learned by reading the Ruby on Rails 6 Practical Guide. It seems to be difficult to read because it is excerpted and picked up. Excuse me. This book also has a sequel Extension, and both books have been studied at the stage of writing the article. I will write an article for review as well. It is divided into 18 chapters, so we will divide it by heading. Up to Chapter 3, the environment construction is the main, so I skipped it.
Chapter4 RSpec
What is the difference between skip and pending? [RSpec]
namespace
routes.rb
namespace :staff do
root "top#index"
end
namespace is a method to set the namespace. In the above example, the root path would be / staff. The controller will be Staff :: TopController.
render
staff/top_controller.rb
def index
render action: "index"
end
render is a method that generates an HTML document. render action: "index" is optional.
staff/top_controller.rb
def index
end
If you use render etc., you can use a template other than the one corresponding to the action. This was also used in the latter half of this book.
present?
present? is the negation of blank ?. An instance method of the Object class added by Rails.
layout
application_controller.rb
class ApplicationController < ActionController::Base
layout :set_layout
private def set_layout
if params[:controller].match(%r{\A(staff|admin|customer)/})
Regexp.last_match[1]
else
"customer"
end
end
end
You can specify a method to determine the layout with the layout method. In the above example, the layout is dynamically switched for each namespace.
rescue_from
application_controller.rb
rescue_from Forbidden, with: :rescue403
If this is defined in the definition of the ApplicationController class, when an exception Forbidden (or an exception that is a descendant of it) occurs in any action, the action is interrupted and the process is transferred to the method rescue403.
application_controller.rb
#Implementation example of method rescue403
private def rescue403(e)
@exception = e
render template: "errors/forbidden", status: 403
end
haml:forbidden.html.haml
- case @exception
- when ApplicationController::IpAddressRejected
"Your IP address(#{request.id})Not available from."
- else
"You do not have permission to view the specified page."
ActiveSupport::Concern You can use a mechanism called ActiveSupport :: Concern to extract a piece of code from one class into another module.
concerns/error_handlers.rb
module ErrorHandlers
extend ActiveSupport::Concern
included do
rescue_from ApplicationController::Forbidden, with: rescue403
#Other codes omitted
end
private def rescue403(e)
@exception = e
render "errors/forbidden", status: 403
end
#Other codes omitted
end
For modules placed in the concerns directory
extend ActiveSupport::Concern
Is required. As a result, the included method is available. This method takes a block so that the code inside the block is evaluated in the context of the class that loaded this module.
Another property is that if you define a class called ClassMethods as a subclass of this module, that method will be included as a class method of the class that loaded the module, but this time it was not used.
application_controller.rb
include ErrorHandlers if Rails.env.production?
The environment to be included is limited so that the original error screen is displayed except in the production environment.
We will add the URLs of the following articles one by one. Ruby on Rails6 Practical Guide cp7 ~ cp9 [Memo] Ruby on Rails6 Practical Guide cp10 ~ cp12 [Memo] Ruby on Rails6 Practical Guide cp13 ~ cp15 [Memo] Ruby on Rails6 Practical Guide cp16 ~ cp18 [Memo] Ruby on Rails6 Practical Guide [Extensions] cp3 ~ cp6 [Memo] Ruby on Rails6 Practical Guide [Extensions] cp7 ~ cp9 [Memo] Ruby on Rails6 Practical Guide [Extensions] cp10 ~ cp12 [Memo]
Recommended Posts