[Rails Tutorial Chapter 4] Rails-flavored Ruby

Default value of argument

The arguments specified when the method is called are assigned in order from the beginning on the method side. At this time, if there is no object to be assigned (the number of arguments specified by the caller is less than the arguments specified by the method definition side), If a default value is set, that value will be substituted instead.

def printHello(msg="No msg", name="No name")
  print(msg + "," + name + "¥n")
end

printHello("Hello", "Sato") #=>Hello,Sato
printHello("How are you") #=> How are you,No name
printHello() #=> No msg, No name

def printHello(msg="No msg", name="No name")
  print(Kconv.tosjis(msg + "," + name + "¥n"))
end



Custom helper

Rails views have a huge number of built-in functions, but you can also create new ones. If you're just writing Ruby code, it's normal to explicitly load and use it every time you create a module. Rails automatically loads the helper module, so There is no need to write the include line.

app/helpers/application_helper.rb


module ApplicationHelper

  #Returns the full title per page.
  def full_title(page_title = '')
    base_title = "Ruby on Rails Tutorial Sample App"
    if page_title.empty?
      base_title
    else
      page_title + " | " + base_title
    end
  end
end


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


<!DOCTYPE html>
<html>
  <head>
    <title><%= full_title(yield(:title)) %></title> <!-- full_You can use the tile method-->
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>

    <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
  </head>

  <body>
    <%= yield %>
  </body>
</html>

Recommended Posts

[Rails Tutorial Chapter 4] Rails-flavored Ruby
rails tutorial Chapter 6
rails tutorial Chapter 1
rails tutorial Chapter 5
rails tutorial Chapter 10
rails tutorial Chapter 9
rails tutorial Chapter 8
Chapter 4 Rails Flavored Ruby
Rails Tutorial Chapter 5 Notes
Rails Tutorial Chapter 10 Notes
Rails Tutorial Chapter 3 Notes
Rails Tutorial Chapter 3 Learning
Rails Tutorial Memorandum (Chapter 3, 3.1)
Rails Tutorial Chapter 4 Notes
Rails Tutorial Chapter 4 Learning
Rails Tutorial Chapter 1 Learning
Rails Tutorial Chapter 2 Learning
Rails Tutorial Chapter 8 Notes
Rails Tutorial Memorandum (Chapter 3, 3.3.2)
rails tutorial
rails tutorial
rails tutorial
rails tutorial
rails tutorial
rails tutorial
rails tutorial
[Rails Struggle/Rails Tutorial] Summary of Rails Tutorial Chapter 2
[Rails Tutorial Chapter 5] Create a layout
rails tutorial chapter 10 summary (for self-learning)
Rails Tutorial (4th Edition) Memo Chapter 6
Rails Tutorial 6th Edition Learning Summary Chapter 10
Rails Tutorial 6th Edition Learning Summary Chapter 7
Rails Tutorial 6th Edition Learning Summary Chapter 4
Rails tutorial test
Rails Tutorial 6th Edition Learning Summary Chapter 9
Rails Tutorial 6th Edition Learning Summary Chapter 6
Rails tutorial memorandum 1
Rails Tutorial 6th Edition Learning Summary Chapter 5
Rails tutorial memorandum 2
Rails Tutorial 6th Edition Learning Summary Chapter 2
Rails Tutorial 6th Edition Learning Summary Chapter 3
Start Rails Tutorial
[Beginner] Rails Tutorial
Rails Tutorial 6th Edition Learning Summary Chapter 8
[Rails Struggle/Rails Tutorial] What you learned in Rails Tutorial Chapter 6
[Rails Struggle/Rails Tutorial] What you learned in Rails Tutorial Chapter 3
Chewing Rails Tutorial [Chapter 3 Creating Almost Static Pages]
[Rails tutorial] A memorandum of "Chapter 11 Account Activation"
Rails Tutorial cheat sheet
[Ruby on Rails] Rails tutorial Chapter 14 Summary of how to implement the status feed
[Rails] Learning with Rails tutorial
Ruby on Rails Elementary
Ruby On Rails Association
Ruby, Rails finally installed
rails tutorial fighting notes Ⅲ
Resolve Gem :: FilePermissionError when running gem install rails (Rails Tutorial Chapter 1)
Ruby on Rails5 Quick Learning Practice Guide 5.2 Compatible Chapter2
Ruby on Rails5 Quick Learning Practice Guide 5.2 Compatible Chapter3
Rails Tutorial 4th Edition: Chapter 1 From Zero to Deployment
Resolve ActiveRecord :: NoDatabaseError when doing rails test (Rails tutorial Chapter 3)
Ruby on Rails Tutorial Troublesome notes when running on Windows