Ruby on Rails6 Practical Guide cp10 ~ cp12 [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.

Previous article Ruby on Rails6 Practical Guide cp4 ~ cp6 [Memo] Ruby on Rails6 Practical Guide cp7 ~ cp9 [Memo]

Chapter 10 View, create, update, and delete records

try

This method returns nil if the receiver is nil, otherwise executes the method with the name specified in the first argument. The second and subsequent arguments are method arguments.

user.try(:name=, "foo")

In the above example, if user is not nil, name = will be executed with user as the receiver.


Specify the URL in the array

  def show
    staff_member = StaffMember.new(params[:id])
    redirect_to [ :edit, :admin, staff_member ]
  end

You can specify the URL in an array as above. The routing name is deduced from the elements of the array and the URL path is generated. In the above example, Rails presumes that the routing name is edit_admin_staff_member and derives the URL / admin / staff_member /: id / edit.


after(css)

label.require:after {
  content: "*";
  padding-left: 6px;
  color: $red;
}

If you add: after after the css selector, the element specified in content will be added after the element. In the above example, an asterisk will be added after the lebel element of the require class.

Chapter 11 Strong Parameters

Mass assignment vulnerability

With a rudimentary knowledge of programming, you can rewrite attributes that shouldn't be freely rewritten. This vulnerability is called a mass assignment vulnerability </ strong>. Strong Parameters </ strong> is a countermeasure introduced in Rails 4.0 against this vulnerability.


Strong Parameters

params.require(:login_form).permit(:email, :password)

params is a method that returns a params object. Calling the require method checks if the params object has a key called: login_form. If you do not have it, you will get the exception ActionController :: ParameterMissing. Calling the permit method on the return value of the require method removes the parameters not specified in the argument.


FactroyBot settings

spec/rails_helper.rb


#abridgement
RSpec.configure do |config|
  #abridgement
  config.include FactroyBot::Syntax::Methods
end

If you try to call FactroyBot without the above settings,

user = FactroyBot.create(name: "foo")

You need to call it like By adding the setting, FactroyBot can be omitted as shown below.

user = create(name: "foo")

Chapter 12 Access control

skip_before_action The class method skip_before_action prevents the method specified in the argument from being executed before the action.

skip_before_action :authorize

It can be used when you want to skip the before_action specified in the parent class.


Shared Exemple

shared_examples

shared_examples "a protected admin controller" do |controller|
  let(:args) do
    {
      host: Rails.application.config.baukis2[:admin][:host],
      controller: controller
    }
  end

  describe "#index" do
    example "Redirect to login form" do
       get url_for(args.merge(action: :index))
       expect(response).to redirect_to admin_login_url
    end
  end
end

The shared_examples method allows multiple spec files to reuse the same example. In the above example, which controller's index action is not specified, so it can be shared by multiple files.

Usage example </ strong>

describe "Staff management by manager", "Before login" do
  include_examples "a protected admin controller", "admin/staff_member"
end

include_examples specifies the name of shared_examples as the first argument. The second and subsequent arguments are taken in as block arguments. In the above example, controller is specified.

Second argument of describe

In the above example, describe has a second argument. This represents the context. It can be rewritten as below.

describe "Staff management by manager" do
  context "Before login" do
    include_examples "a protected admin controller", "admin/staff_member"
  end
end

travel_to

The travel_to method of ActiveSupport :: Testing :: TimeHelpers moves the current time to the specified point in time.

To include ActiveSupport :: Testing :: TimeHelpers in RSpec, you need to include it in rails_helper.

spec/rails_helper.rb


#abridgement
RSpec.configure do |config|
  #abridgement
  config.include ActiveSupport::Testing::TimeHelpers
end
travel_to 60.minutes.from_now

In the example above, the current time is advanced by 60 minutes.

travel_back 60.minutes.ago

You can also use travel_back to move the current time back 60 minutes.

Continued

We will add the URLs of the following articles one by one.

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