I don't know anything, I tried to make TODO while studying ruby ​​on rails for 3 days by myself

Introduction

Learn more about how I studied ruby ​​on rails!

Why did you study ruby?

I heard that I sometimes use ruby ​​in Japan. I started studying because I was about to meet the ruby ​​project someday.

How to study ruby

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)

How to study rails

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

Make mysql

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 :)

The worst problem I encountered while studying

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.

Problem 1

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)

Problem 1 solution

Add code to Gemfile

gem 'mysql2'
bundle install

Problem 2

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

Problem 2 solution

bundle config --local build.mysql2 "--with-mysql-dir=C:\Users\dev\mysql-connector-c-noinstall-6.0.2-win32"
bundle install

Problem 3

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

Problem 3 solution

Modified to the code in Gemfile

gem 'mysql2', '~> 0.4.10'

Problem 4

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

What is WSL?

https://qiita.com/Brutus/items/f26af71d3cc6f50d1640

See the link above for more information on WSL

I used WSL to connect ruby ​​on rails and mysql.

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.

Programming slowly one by one

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

contents.png

users table

users.png

Rails session cannot be saved

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.

Causes of problems where session cannot be saved when using POST METHOD

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.

csrf token problem solving

https://qiita.com/naberina/items/d3b14521e78e0daccdcd

I found out how to send a csrf token normally with a stomach link.

result

After overcoming various problems, I was able to complete it in 3 days! !!

version1.png

At first, the design is not good.

I focused on creating features.

version2.gif

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.

version3_new.gif

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

Summary

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

I don't know anything, I tried to make TODO while studying ruby ​​on rails for 3 days by myself
I tried to make it possible to set the delay for the UDP client of Android by myself
I tried to make a new sorting algorithm, but I don't know if it's really new
[Rails] I don't know how to use the model ...
[Ruby on Rails] Since I finished all Rails Tutorial, I tried to implement an additional "stock function"
I tried installing Ruby on Rails related plugin with vim-plug
I tried to introduce Bootstrap 4 to the Rails 6 app [for beginners]
What I was addicted to while using rspec on rails
I tried using Hotwire to make Rails 6.1 scaffold a SPA
I want to add a browsing function with ruby on rails
[RSpec on Rails] How to write test code for beginners by beginners
[Java] I tried to make a maze by the digging method ♪
I tried to make Numeron which is not good in Ruby
[Ruby on Rails] How to avoid creating unnecessary routes for devise
[Ruby on Rails] Pass the parameters divided by date_select to FormObject.
I tried to make a group function (bulletin board) with Rails
How to use Ruby on Rails
I tried to make a parent class of a value object in Ruby
How to build a Ruby on Rails environment using Docker (for Docker beginners)
Ruby on Rails DB Tips for creating methods to reduce the load
I tried to make full use of the CPU core in Ruby
Ruby on Rails When you don't know the cause of rollback when saving.