Ruby on Rails6 Practical Guide cp4 ~ cp6 [Memo]

Introduction

The author of this article is a beginner just starting to learn programming. I would appreciate it if you could point out any mistakes.

Overview

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]

Chapter5 Visual Design

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.

Chapter6 Error Page

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
Template example (partial excerpt)

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."

Extraction of error handling module

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.

Limit the environment in which the code is executed

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.

Continued

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]

Quote source

Recommended Posts

Ruby on Rails6 Practical Guide cp13 ~ cp15 [Memo]
Ruby on Rails6 Practical Guide cp7 ~ cp9 [Memo]
Ruby on Rails6 Practical Guide cp4 ~ cp6 [Memo]
Ruby on Rails6 Practical Guide cp10 ~ cp12 [Memo]
Ruby on Rails6 Practical Guide cp16 ~ cp18 [Memo]
Ruby on Rails6 Practical Guide [Extensions] cp7 ~ cp9 [Memo]
Ruby on Rails6 Practical Guide [Extensions] cp10 ~ cp12 [Memo]
Ruby on Rails6 Practical Guide [Extensions] cp3 ~ cp6 [Memo]
Ruby on Rails 6.0 environment construction memo
Ruby on Rails Elementary
Ruby on Rails basics
Ruby On Rails Association
[Personal memo] Ruby on Rails environment construction (Windows)
Ruby on rails learning record -2020.10.03
Portfolio creation Ruby on Rails
Ruby on rails learning record -2020.10.04
Ruby on Rails5 Quick Learning Practice Guide 5.2 Compatible Chapter2
[Ruby on Rails] Debug (binding.pry)
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby on Rails config configuration
Ruby on Rails basic learning ①
Ruby on rails learning record-2020.10.07 ②
Ruby on Rails5 Quick Learning Practice Guide 5.2 Compatible Chapter3
Ruby on rails learning record-2020.10.07 ①
Cancel Ruby on Rails migration
Ruby on rails learning record -2020.10.06
Ruby on Rails validation summary
Ruby on Rails Basic Memorandum
Ruby on Rails Overview (Beginner Summary)
[Ruby on Rails] Read try (: [],: key)
[Ruby on Rails] yarn install --check-files
Ruby on Rails variable, constant summary
Installing Ruby + Rails on Ubuntu 18.04 (rbenv)
[Ruby on Rails] Introduced paging function
Basic knowledge of Ruby on Rails
Progate Ruby on Rails5 Looking Back
How to use Ruby on Rails
[Ruby on Rails] Add / Remove Columns
Ruby on Rails Japanese-English support i18n
(Ruby on Rails6) "Erase" posted content
[Ruby on Rails] CSV output function
[Ruby on Rails] What is Bcrypt?
[Ruby on Rails] Confirmation page creation
[Ruby on Rails] Comment function implementation
[Ruby on Rails] DM, chat function
[Ruby on Rails] Convenient helper method
[Ruby on Rails] Stop "looping until ..."
Ruby memo
[Ruby on Rails] Introduction of initial data
[Ruby on Rails] Search function (not selected)
[Rails] Addition of Ruby On Rails comment function
[Ruby on Rails] Creating an inquiry form
[Ruby on Rails] View test with RSpec
[Ruby on Rails] How to use CarrierWave
[Ruby on Rails] 1 model CRUD (Routing Main)
Ruby on Rails installation method [Mac edition]
[Ruby on Rails] model, controller terminal command
Let's summarize "MVC" of Ruby on Rails
Ruby on Rails model creation / deletion command
[Ruby on Rails] About bundler (for beginners)