[RUBY] How to use custom helpers in rails

Quote Rails Tutorial

What is a custom helper?

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.

File location

app/helpers/application_helper.rb


module ApplicationHelper

end

The location of the file is app/helpers/application_helper.rb Initially, the file structure looks like this.

How to use

For example, write like this.

app/helpers/application_helper.rb


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.

ruby:app/views/layouts/application.html.erb


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

Recommended Posts

How to use custom helpers in rails
How to use MySQL in Rails tutorial
How to use credentials.yml.enc introduced in Rails 5.2
[Rails] How to use enum
[Rails] How to use select boxes in Ransack
[Rails] How to use enum
How to use rails join
How to use JQuery in js.erb of Rails6
[Rails] How to use validation
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails] How to use PostgreSQL in Vagrant environment
[Rails] How to use Scope
[Rails] How to use gem "devise"
How to use Lombok in Spring
[Rails] How to use devise (Note)
[Rails] How to use flash messages
[Rails] How to write in Japanese
How to use Ruby on Rails
How to use InjectorHolder in OpenAM
[Rails] How to use Active Storage
How to introduce jQuery in Rails 6
How to use classes in Java?
[Introduction to Rails] How to use render
How to install Swiper in Rails
How to implement search functionality in Rails
Multilingual Locale in Java How to use Locale
How to change app name in rails
[Ruby on Rails] How to use CarrierWave
How to use named volume in docker-compose.yml
[Rails] How to use rails console with docker
How to insert a video in Rails
[Rails] How to use ActiveRecord :: Bitemporal (BiTemporalDataModel)
[Rails] How to use the map method
How to use Docker in VSCode DevContainer
[rails] How to configure routing in resources
[Ruby on Rails] How to use redirect_to
How to implement ranking functionality in Rails
[Note] How to use Rails 6 Devise + cancancan
[Ruby on Rails] How to use kaminari
How to use environment variables in RubyOnRails
[Rails] How to use video_tag to display videos
Understand in 5 minutes !! How to use Docker
[Rails] How to use helper method, confimartion
How to use ExpandableListView in Android Studio
How to use Map
How to write Rails
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
How to uninstall Rails
[How to use label]