Learn more about how I studied ruby on rails!
I heard that I sometimes use ruby in Japan. I started studying because I was about to meet the ruby project someday.
https://www.tutorialspoint.com/ruby/index.htm
I've checked all the examples in the link above. I studied while doing only the important points! If I don't understand something, I will refer to it later.
# variable and operate
num1 = 10
num2 = 20
result = num1 + num2
# print string
puts "result is #{result}"
# if else condition
if result > 10
puts "result > 10"
else
puts "else condition"
end
# loop
# start <= n <= end
for n in 1..5
puts "#{n}"
end
# array (list)
arr = [1, 2, 3]
for n in arr
puts "#{n}"
end
# hash (dict)
hash = Hash[
"a" => 100,
"b" => 200
]
puts "#{hash['a']}"
puts "#{hash['b']}"
# block
def helloworld
puts "hello world"
end
helloworld()
def add(num1, num2)
puts "sum is #{num1 + num2}"
end
add(5,8)
https://guides.rubyonrails.org/getting_started.html
I partially referred to the example in the link above.
install rails
gem install rails
Check if rails was installed well
rails --version
Rails 6.1.0
Make a project
rails new todo
See options
rails s --help
Run the server
rails s
docker-compose.yml
version: '3'
services:
db:
image: mysql:8.0
container_name: mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: todo
MYSQL_USER: docker
MYSQL_PASSWORD: docker
TZ: 'Asia/Tokyo'
command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
ports:
- 3306:3306
docker-compose up -d
I made mysql easily using docker :)
https://rubyinstaller.org/downloads/
I downloaded ruby from the link above.
ruby --version
ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x64-mingw32]
gem --version
3.2.3
At first I didn't understand at all. It was useless to use ruby on windows10 ...
I ran into a problem when connecting with mysql.
Puma caught this error: Error loading the 'mysql2' Active Record adapter. Missing a gem it depends on? mysql2 is not part of the bundle. Add it to your Gemfile. (LoadError)
Add code to Gemfile
gem 'mysql2'
bundle install
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
...
Cannot find include dir(s)
C:\Users\dev\mysql-connector-c-noinstall-6.0.2-win32/include/include
bundle config --local build.mysql2 "--with-mysql-dir=C:\Users\dev\mysql-connector-c-noinstall-6.0.2-win32"
bundle install
client.c:1350:38: error: 'MYSQL_DEFAULT_AUTH' undeclared (first use in this
function); did you mean 'MYSQL_SECURE_AUTH'?
1350 | return _mysql_client_options(self, MYSQL_DEFAULT_AUTH, value);
| ^~~~~~~~~~~~~~~~~~
| MYSQL_SECURE_AUTH
Modified to the code in Gemfile
gem 'mysql2', '~> 0.4.10'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require': cannot load such file -- Win32API (LoadError)
Did you mean? win32/sspi
I gave up here ... I found out after a lot of research I understand the fact that everyone uses WSL. From here I programmed with WSL
https://qiita.com/Brutus/items/f26af71d3cc6f50d1640
See the link above for more information on WSL
Install dependency
sudo apt-get update
sudo apt-get install autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm6 libgdbm-dev libdb-dev
Install rbenv
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
Set rbenv
vi ~/.bashrc
export PATH=$PATH:$HOME/.rbenv/bin
eval "$(rbenv init -)"
source ~/.bashrc
ruby setting
rbenv --version
rbenv install --list
rbenv install 3.0.0
rbenv versions
rbenv global 3.0.0
rbenv versions
install db dependency
sudo apt-get install libsqlite3-dev
sudo apt install libmysqlclient-dev
Add the code below to the Gemfile
gem 'mysql2'
install bundle
bundle install
You can now connect with mysql.
The first thing I had to do to render the html was to create a controller.
rails generate controller Todo index --skip-routes
After checking the html rendering, I worked hard on the code.
After that, I made a simple model and modified the columns with mysql.
rails generate model Todo content:string
rails generate model User username:string
rails db:migrate
contents table
users table
It works when session is saved by GET METHOD and session is called by GET METHOD. If you save the session with GET METHOD and call session with POST METHOD, it will not work.
https://stackoverflow.com/questions/18422182/rails-sessions-not-saving
It turns out that if the csrf tokens don't match, the session will be initialized.
https://qiita.com/naberina/items/d3b14521e78e0daccdcd
I found out how to send a csrf token normally with a stomach link.
After overcoming various problems, I was able to complete it in 3 days! !!
At first, the design is not good.
I focused on creating features.
After that, I made a reactive web using the break point and space of bootstrap. I also modified the design and added the toastr library.
Finally, I made a function using session to study login. The ui that changes the user used slick.js.
github https://github.com/h4ppyy/ror-todo
For web study, creating TODO is effective because you can quickly create, read, update, and delete. In my case, I was able to experience different languages and frameworks faster in this way!
Recommended Posts