Ruby on Rails variable, constant summary

Ruby on Rails variable, constant summary

We have summarized the variables and constants that are often used in Rails.

Prerequisites

--Variables --Can be changed --Constant --Constant not possible

variable

Local variables

--Valid only within methods and blocks

Utilization with Rails

<% hoge = 'huga' %>
<div><%= hoge %><div>
def create
  article = Article.find( params[:id])
  return render_404 if article.blank?
end

Instance variables

--Variables that can be used in the same controller

Utilization with Rails

--Pass the instance variable defined in Controller Action to View.

def show
  @user = User.find(params[:id])
end

[users/show.html.erb]

<div>USER ID:<%= @user.id %><div>
def show
  user = User.find(params[:id])
end

[users/show.html.erb]

<div>USER ID:<%= user.id %><div>

-> Cannot be referenced undefined local variable or method 'user'

Global variables

--All Controllers can be used --Can be changed and referenced from anywhere in the program

constant

--Define in class

--Constant name --Capital letters, snake case

Utilization with Rails

--Define common constants in [config / initializers / constants.rb] --Can be referenced in all Controllers and Views [config/initializers/constants.rb]

MAX_SIZE = 10

--To define the constants used in each Model, define them in the class of the model file. [user.rb]

class User
  OFFICIAL_ID = 100
end

When referencing with Controller or View, write ʻUser :: OFFICIAL_ID`

Recommended Posts

Ruby on Rails variable, constant summary
Ruby on Rails validation summary
Ruby on Rails Overview (Beginner Summary)
ruby constant variable
Ruby on Rails Elementary
Ruby on Rails basics
Ruby On Rails Association
Portfolio creation Ruby on Rails
Ruby on rails learning record -2020.10.04
[Ruby on Rails] Debug (binding.pry)
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby on Rails config configuration
Ruby on Rails basic learning ①
[Ruby on Rails] about has_secure_password
Ruby on rails learning record-2020.10.07 ②
Commentary on partial! --Ruby on Rails
Ruby on rails learning record-2020.10.07 ①
Cancel Ruby on Rails migration
Ruby on rails learning record -2020.10.06
Ruby on Rails Basic Memorandum
Ruby on Rails Refactoring method example summary around MVC
[Ruby on Rails] Read try (: [],: key)
Installing Ruby + Rails on Ubuntu 18.04 (rbenv)
[Ruby on Rails] Introduced paging function
Basic knowledge of Ruby on Rails
Progate Ruby on Rails5 Looking Back
Ruby on Rails for beginners! !! Summary of new posting functions
How to use Ruby on Rails
[Ruby on Rails] Add / Remove Columns
Ruby on Rails Japanese-English support i18n
(Ruby on Rails6) "Erase" posted content
[Ruby on Rails] CSV output function
Ruby on Rails 6.0 environment construction memo
[Ruby on Rails] What is Bcrypt?
[Ruby on Rails] Confirmation page creation
Ruby On Rails devise routing conflict
[Ruby on Rails] Comment function implementation
[Ruby on Rails] DM, chat function
[Ruby on Rails] Convenient helper method
[Ruby on Rails] Stop "looping until ..."
Ruby on Rails for beginners! !! Post list / detailed display function summary
[Ruby on Rails] "|| =" ← Summary of how to use this assignment operator
[Ruby on Rails] Introduction of initial data
[Ruby on Rails] Search function (not selected)
[Rails] Addition of Ruby On Rails comment function
[Ruby on Rails] Creating an inquiry form
Ruby on Rails6 Practical Guide cp13 ~ cp15 [Memo]
[Ruby on Rails] View test with RSpec
[Ruby on Rails] How to use CarrierWave
[Ruby on Rails] Code check using Rubocop-airbnb
[Ruby on Rails] 1 model CRUD (Routing Main)
Ruby on Rails installation method [Mac edition]
[Ruby on Rails] model, controller terminal command
Let's summarize "MVC" of Ruby on Rails
Ruby on Rails model creation / deletion command
[Ruby on Rails] About bundler (for beginners)
part of the syntax of ruby ​​on rails
Ruby on Rails6 Practical Guide cp7 ~ cp9 [Memo]
Ruby on Rails in Visual Studio Codespaces
[Ruby on Rails] Follow function implementation: Bidirectional