[RUBY] Rails learning day 3

Ruby on Rails5 Quick Learning Practice Guide chapter6

6-2-1 Five elements that make up the "route"

Routing specifies which action the request should be executed. Here's what you need to configure your routing:

Element name Example of element content Explanation
HTTP method GET,POST,PATCH,PUT,DELETE An object that represents the method of transmitting and acquiring information
URL pattern /tasks、/tasks/:id etc. URL itself
URL pattern name new_task, tasks, etc. A unique name for each defined URL pattern
controller tasks(Taskcontroller) 呼びたいアクションのcontrollerクラスを指定する
action index 呼びたいactionを指定する

Define a set of CRUD routes with 6-2-5 resources

This time, which explained resources in the previous explanation, is an application of it.

/config/routes.rb


resouces :tasks

When described in this way, the seven HTTP methods of task routing are automatically combined into one. I wrote that. So what if you want to write other routes? Use collection when you want to add a plus

/config/routes.rb


resouces :tasks do
  collection do
    get 'export'
  end
end

By writing the HTTP method and action name in the collection in this way, routing will work automatically.

6-3 Internationalization

It is possible to write code in Japanese by programming with a ja.yml file. Users will also be able to use the service in Japanese, but foreigners may have the opportunity to use it. It may be necessary to change the language depending on the user. I18n.locale is used in such a case. I18n makes it possible to use one program for multiple countries.

class ApplicationController < ActionController::Base
  before_action :set_locale


private

  def set_locate
    I18n.locale = current_user&.locale || :ja #Japanese if you are not logged in
  end
end

You can use it by defining it in the controller

6-6 Rails log

A log is a history when you do something. If there are any errors or bugs, you can go back in the log to see what went wrong. For example, if you want to log the task information saved when creating a task, do as follows.

app/controllers/tasks_controller.rb


   def create

     if @task.save
       logger.debug "task: #{@task.attributes.inspect}"
       redirect_to @task, notice: "task"#{@task.name}Was registered"
     else

inspect is something that displays in an easy-to-understand manner.

However, there is some personal information that should not be recorded in the log, and at that time it must be prevented from appearing in the log.

config/initializers/filter_parameter_logging.rb


Rails.application.config.filter_parameter +=[:password]

The value of the parameter specified here is displayed as [FILTERED] on the log.

Recommended Posts

Rails learning day 3
Rails learning day 4
Rails learning day 2
rails learning day 1
Rails learning day 2 part 2
Rails learning day 1 part 3
Rails learning day 3 part 2
Rails learning Day 1 Part 2
Programming learning day 3
java learning day 2
java learning day 1
Rails Tutorial Chapter 3 Learning
[Rails] Learning with Rails tutorial
Rails Tutorial Chapter 4 Learning
Rails Tutorial Chapter 1 Learning
Rails Tutorial Chapter 2 Learning
Ruby on rails learning record -2020.10.04
Ruby on rails learning record -2020.10.05
4th day of java learning
Ruby on Rails basic learning ①
Ruby on rails learning record-2020.10.07 ②
Ruby on rails learning record-2020.10.07 ①
Ruby on rails learning record -2020.10.06
rails learning rake task, cron, whenever
[Rails g.error]
Rails Tutorial 6th Edition Learning Summary Chapter 10
Java learning (0)
Ruby learning 4
Rails Tutorial 6th Edition Learning Summary Chapter 7
Rails basics
Rails API
Rails migration
Rails Tutorial 6th Edition Learning Summary Chapter 4
[Rails] first_or_initialize
Java Day 2018
rails tutorial
Rails Tutorial 6th Edition Learning Summary Chapter 9
Rails Tutorial 6th Edition Learning Summary Chapter 6
About Rails 6
Servlet learning
Ruby learning 3
Rails foundation
Rails Tutorial 6th Edition Learning Summary Chapter 5
Rails memorandum
rails tutorial
rails tutorial
rails tutorial
Learning output ~ 11/3 ~
Rails Tutorial 6th Edition Learning Summary Chapter 2
Rails Tutorial Chapter 0: Preliminary Basic Knowledge Learning 5
Ruby learning 2
Maven learning
[Rails] devise
Ruby learning 6
rails tutorial
Rails Tutorial 6th Edition Learning Summary Chapter 3
rails tutorial
Learning output
Rails Tips
rails method
rails tutorial