progate無料版を利用して基本的なことを学ぶ
プログラミングを学ぶためにprogete(課金なし)を利用させて頂いているが、初心者にもすごく理解しやすく作られている。
初心者が初心者にオススメするprogate
今回はローカルにWEBサイトを作成する為にRubyOnRailsの学習で1週した(無料部分のみ)。
初心者にも理解しやすく作られている分、1週するのにそれなりの時間はかかる為、2週目はしたくないが忘れてしまうので自分用に要約しておく。
環境設定まではできている段階から始めるので、できていない場合は以前のブログ記事を参考に。
RubyOnRailsの環境設定(virtualbox)覚え書き
# homeコントローラーを作成する
[root@host02 intra_net]# rails generate controller home top
[root@host02 intra_net]# ls
app config.ru Gemfile.lock package.json README.md tmp bin db lib public storage vendor config Gemfile log Rakefile test
# viewsフォルダにhomeフォルダが作成された
[root@host02 intra_net]# ls app/views
'home' layouts
# homeフォルダにtop.html.erbファイルが作成された
[root@host02 intra_net]# ls app/views/home
'top.html.erb'
# controllersフォルダにhome_controller.rbファイルが作成された
[root@host02 intra_net]# ls app/controllers
application_controller.rb concerns 'home_controller.rb'
home_controller.rb
class HomeController < ApplicationController
def top
end
end
ルーティングの設定はroutes.rbファイルで
# configフォルダの中のroutes.rbファイル
[root@host02 intra_net]# ls config
application.rb credentials.yml.enc environments master.key
spring.rb boot.rb database.yml initializers puma.rb
storage.yml cable.yml environment.rb locales 'routes.rb'
コントローラー作成時に自動でルーティングの設定がされている
routes.rb
Rails.application.routes.draw do
get 'home/top'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
スタイルシートはapp/assets/stylesheetsフォルダに作成されている
[root@host02 intra_net]# ls app/assets/stylesheets
application.css 'home.scss'
サイトのページを増やす場合は、
- routes.rbにルーティングを追記する
- home_controller.rbにアクションを追加する
- views/コントローラー名/アクション名.html.erb ファイルを作成する
routes.rb
Rails.application.routes.draw do
get 'home/top'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
get 'about' => 'home#about'
home_controller.rb
class HomeController < ApplicationController
def top
end
def about
end
end
[root@host02 intra_net]# ls app/views/home
top.html.erb
[root@host02 intra_net]# touch app/views/home/about.html.erb
[root@host02 intra_net]# ls app/views/home
about.html.erb top.html.erb
必要な画像はpublicフォルダへ
[root@host02 intra_net]# ls public
404.html 500.html apple-touch-icon-precomposed.png robots.txt 422.html apple-touch-icon.png favicon.ico