ラベル Ruby の投稿を表示しています。 すべての投稿を表示
ラベル Ruby の投稿を表示しています。 すべての投稿を表示

Rails:学習:1.3.4 Hello, world!


hello, world! を表示させる

■controller修正
ブラウザーからのリクエストはコントローラが処理する。
コントローラが"hello, world!"を返すように修正する。

コントローラーはアプリケーションフォルダ(environment/hello_app/)の "app/controllers/" に "*_contoller.rb" のファイル名で存在している。
このファイルを修正する。

application_controller.rb
class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  def hello
    render html: "hello, world!"
  end
end
■ルーター修正
Railsのデフォルトページでなく、修正したコントローラのアクションを使うようにルーターを修正する。 ルーターはコントローラとブラウザの間に配置され、ブラウザからのリクエストをコントローラに振り分ける(ルーティング)役割を果たす。 ルーティングファイルは config/routes.rb 今回の修正では ・コントローラ名:application ・アクション名:hello 上記を踏まえ、ルーティングファイルを修正
enrivonment/hello_app/config/routes.rb
"ルート(root)"のルーティングをapplicationのhelloに割り当てる
Rails.application.routes.draw do
    # For details on the DSL available within this file, see http://guides.rubyonr
  
  ails.org/routing.html
    root 'application#hello'
  end
■動作確認
rails serverを起動しブラウザで表示してみる → OK
[Mac][Ruby][Rails]

Rails:学習:1.3.最初のアプリケーション


Hello Worldプログラムを作ってみる

■environmentディレクトリ作成
練習プログラムはホームディレクトリ配下のenvironmentディレクトリに作成することとする。
ということでディレクトリを作成し移動
cd
mkdir environment
cd environment/
(base) users-iMac:~ user$ cd
(base) users-iMac:~ user$ mkdir environment
(base) users-iMac:~ user$ cd environment/
(base) users-iMac:environment user$

■プロジェクト作成
rails 5.1.6 で hello_app アプリを作成する
rails _5.1.6_ new hello_app
(base) users-iMac:environment user$ cd ~/environment
(base) users-iMac:environment user$ rails _5.1.6_ new hello_app
      create
      create  README.md
 :
* For more details, please refer to the Sass blog:
  http://sass.logdown.com/posts/7081811

          run  bundle exec spring binstub --all
* bin/rake: spring inserted
* bin/rails: spring inserted

■Gemfile置換
~/environment/hello_app/Gemfileファイルを参考サイトの内容で置換する。

これは、チュートリアルがうまく動くようにバージョン指定を行っているらしい。
以下内容を張り付ける。
source 'https://rubygems.org'

gem 'rails',        '5.1.6'
gem 'puma',         '3.9.1'
gem 'sass-rails',   '5.0.6'
gem 'uglifier',     '3.2.0'
gem 'coffee-rails', '4.2.2'
gem 'jquery-rails', '4.3.1'
gem 'turbolinks',   '5.0.1'
gem 'jbuilder',     '2.6.4'

group :development, :test do
  gem 'sqlite3',      '1.3.13'
  gem 'byebug', '9.0.6', platform: :mri
end

group :development do
  gem 'web-console',           '3.5.1'
  gem 'listen',                '3.1.5'
  gem 'spring',                '2.0.2'
  gem 'spring-watcher-listen', '2.0.1'
end

# Windows環境ではtzinfo-dataというgemを含める必要があります
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

修正したらgemをインストール
※ gem … ライブラリのことらしい
※ bundle … gemの管理プログラムのことらしい
cd ~/environment/hello_app/
bundle install
(base) users-iMac:environment user$ cd ~/environment/hello_app
(base) users-iMac:hello_app user$ bundler install
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler
 :
Resolving dependencies...
Bundler could not find compatible versions for gem "activesupport":
  In snapshot (Gemfile.lock):
    activesupport (= 5.1.6.1)

  In Gemfile:
    rails (= 5.1.6) was resolved to 5.1.6, which depends on
      activesupport (= 5.1.6)

    coffee-rails (= 4.2.2) was resolved to 4.2.2, which depends on
      railties (>= 4.0.0) was resolved to 5.1.6.1, which depends on
        activesupport (= 5.1.6.1)

Running `bundle update` will rebuild your snapshot from scratch, using only
the gems in your Gemfile, which may resolve the conflict.

→ エラーとなった。
activesupport 5.1.6.1 が必要?

■activesupportインストール
gem install activesupport -v 5.1.6.1
(base) users-iMac:hello_app user$ gem install activesupport -v 5.1.6.1
Successfully installed activesupport-5.1.6.1
Parsing documentation for activesupport-5.1.6.1
Installing ri documentation for activesupport-5.1.6.1
Done installing documentation for activesupport after 4 seconds
1 gem installed

もう一度 bundler install を実行
→ 同じエラーとなった

■activesupportインストール
gem uninstall activesupport で削除してみる。
5.1.6が入っていたので5.1.6のみ削除
(base) users-iMac:hello_app user$ gem uninstall activesupport

    Select gem to uninstall:
     1. activesupport-5.1.6
     2. activesupport-5.1.6.1
     3. All versions
    > 1
    
    You have requested to uninstall the gem:
            activesupport-5.1.6
    
    actionpack-5.1.6 depends on activesupport (= 5.1.6)
    actionview-5.1.6 depends on activesupport (= 5.1.6)
    activejob-5.1.6 depends on activesupport (= 5.1.6)
    activemodel-5.1.6 depends on activesupport (= 5.1.6)
    activerecord-5.1.6 depends on activesupport (= 5.1.6)
    rails-5.1.6 depends on activesupport (= 5.1.6)
    railties-5.1.6 depends on activesupport (= 5.1.6)
    If you remove this gem, these dependencies will not be met.
    Continue with Uninstall? [yN]  y
    Successfully uninstalled activesupport-5.1.6

もう一度 bunder install を実行
→ 同じエラーとなった。エラーメッセージをよく見ると
Bundler could not find compatible versions for gem "activesupport":
In snapshot (Gemfile.lock):
activesupport (= 5.1.6.1)
もしかして Gemfile.lock というファイルを消せばよい?

~/environment/hello_app/GEnfile.lock を削除
もう一度 bundler install を実行
今度はうまくいったようだ。
 :
Using turbolinks-source 5.2.0
Fetching turbolinks 5.0.1
Installing turbolinks 5.0.1
Fetching uglifier 3.2.0
Installing uglifier 3.2.0
Fetching web-console 3.5.1
Installing web-console 3.5.1
Bundle complete! 15 Gemfile dependencies, 64 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

これでプログラムは完成。
rainsには簡易webサーバ機能があり結果を確認できる。
パスがhello_appにあることを確認して以下コマンドを実行
rails server
(base) users-iMac:hello_app user$ pwd
  /Users/user/environment/hello_app
  (base) users-iMac:hello_app user$ rails server
  => Booting Puma
  => Rails 5.1.6 application starting in development
 :
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop

ブラウザで http://192.168.56.101:3000 を開いてみる。
→ OK



[Ruby]
https://railstutorial.jp/chapters/beginning?version=5.1#sec-installing_rails

Ruby:Mac:Ruby+Rails再インストール

前回うまくいかなかったので、もう一度トライ。

■環境削除
関係ありそうな以下フォルダをまるごと削除することにした。
~/user/.bundle/
~/user/.gem/
~/user/.rbenv/
~/user/.ruby-version
~/user/environment/
※environmentフォルダは練習で作成したフォルダ

■環境設定確認
前回登録したが、bash_profileにrbenv initが登録されているか確認
登録されていれば最下行に登録されているはず
(base) users-iMac:~ user$ cat ~/.bash_profile
 :
# <<< conda init <<<
eval "$(rbenv init -)"
→ OK 登録されていなければ以下コマンドで登録
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile source ~/.bash_profile
■もう一度Ruby2.4.1をインストール
rbenv install (Rubyバージョン)
(base) users-iMac:~ user$ rbenv install 2.4.1
ruby-build: use openssl from homebrew
Downloading ruby-2.4.1.tar.bz2...
 :
ruby-build: use readline from homebrew
Installed ruby-2.4.1 to /Users/user/.rbenv/versions/2.4.1
■デフォルトで使用するRubyバージョン設定
rbenv global (Rubyバージョン)
(base) users-iMac:~ user$ rbenv global 2.4.1
rbenv versionsで設定確認
(base) users-iMac:~ user$ rbenv versions
  system
* 2.4.1 (set by /Users/user/.ruby-version)
→ 2.4.1に * マークが付いているのでOK
■動作確認
Rubyのバージョンが2.4.1になっているか確認
ruby -v
(base) users-iMac:~ user$ ruby -v
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin17]
→ OK
■railsインストール
※バージョン5.1.6指定でインストール
gem install rails -v 5.1.6
(base) users-iMac:~ user$ gem install rails -v 5.1.6
 :
lties, bundler, sprockets, sprockets-rails, rails after 79 seconds
36 gems installed
バージョン確認
rails -v
(base) users-iMac:~ user$ rails -v
Rails 5.1.6
■bundlerインストール
gem install bundler
(base) users-iMac:~ user$ gem install bundler
Successfully installed bundler-2.0.1
Parsing documentation for bundler-2.0.1
Done installing documentation for bundler after 5 seconds
1 gem installed
■反映?
rbenv rehash
(base) users-iMac:~ user$ rbenv rehash

[Mac][Ruby][Rails]

【旧版】Ruby:Mac:Railsダウングレード


仮想Mac HighSierra (10.13.6)
学習のため最新のRails(5.2.2)をインストールしたが、参考サイトのチュートリアルは5.1.6指定だったためダウングレードを行う。

gem uninstall railties -v '5.2.2'
(base) users-iMac:~ user$ gem uninstall railties -v '5.2.2'

You have requested to uninstall the gem:
        railties-5.2.2

rails-5.2.2 depends on railties (= 5.2.2)
If you remove this gem, these dependencies will not be met.
Continue with Uninstall? [yN]  y
Successfully uninstalled railties-5.2.2

5.1.6インストール
gem install rails -v 5.1.6
(base) users-iMac:~ user$ gem install rails -v 5.1.6
Fetching activesupport-5.1.6.gem
 :
Installing ri documentation for rails-5.1.6
Done installing documentation for activesupport, actionview, actionpack, activem

odel, arel, activerecord, activejob, actionmailer, websocket-driver, actioncable

, railties, rails after 24 seconds
12 gems installed

バージョン確認
rails -v
(base) users-iMac:~ user$ rails -v
Rails 5.1.6

→ OK


[Ruby]

【旧版】Ruby:Mac:Railsインストール


仮想Mac High Sierra (10.13.6)
Rubyをインストールしたので引き続きRailsをインストールする。


gem update --system
gem install rails
gem install bundler
rbenv rehash

■動作確認
rails -v
(base) users-iMac:~ user$ rails -v
Rails 5.2.2


[Ruby]
https://qiita.com/shinyashikis@github/items/3501c5f7f71a8e345c3d

Ruby学習(1)

(1) インストール
https://www.ruby-lang.org/ja/
- [ダウンロード] - [Windows版Rubyバイナリ]
- "RubyInstaller" - [Download]
- "WITH DEVKIT" の "Ruby+Devkit 2.5.1-1 (x64)" をダウンロード

その他の記事