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を指定する |
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.
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
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