Quote Rails Tutorial
Rails has many built-in functions, but you can create your own when you don't have the method you want to use. This newly created method is called a custom helper.
module ApplicationHelper end
The location of the file is app/helpers/application_helper.rb Initially, the file structure looks like this.
For example, write like this.
module ApplicationHelper def full_title(page_title = '') base_title = "Sample App" if page_title.empty? base_title else page_title + " | " + base_title end end end
Describe in the place you want to call the application.html.erb file. This time, write it in the title tag.
<!DOCTYPE html> <html> <head> <title><%= full_title(yield(:title)) %></title> </head> <body> <%= yield %> </body> </html>
If you write it like this, this code written in the helper file will be called in the title tag.
def full_title(page_title = '') base_title = "Sample App" if page_title.empty? base_title else page_title + " | " + base_title end end #Code commentary page by if statement_title is.empty?(Empty container → true,There is a container → false) →true:base_title(Sample App)Show →false:page_title|base_title(Sample App)Show
This time, the amount of code is not large, so it is hard to feel the benefits, but separating the code into separate files makes it easier to see the code.