Railsのモデル名にLoveなどをつけた場合、has_many: lovesと指定すると、Railsはloves => lofeと変換してしまう。
これを正しくするには、inflections.rbに追記する。
config/initializers/inflections.rb
```
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.irregular "love", "loves"
end
```
mysql2のインストール失敗 An error occurred while installing mysql2 (0.4.5), and Bundler cannot continue.
rails5をインストールするときにmysql2のgemがうまく入らずにハマった時のメモ
mac 10.12.3
ruby 2.3.1p112
Bundler version 1.12.5
```
Installing mysql2 0.4.5 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
current directory: /workdir/vendor/bundle/ruby/2.3.0/gems/mysql2-0.4.5/ext/mysql2
/Users/yoshifuji.hiroki/.rbenv/versions/2.3.1/bin/ruby -r ./siteconf20170415-12041-6ex24h.rb extconf.rb
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for ruby/thread.h... yes
checking for rb_thread_call_without_gvl() in ruby/thread.h... yes
checking for rb_thread_blocking_region()... no
checking for rb_wait_for_single_fd()... yes
checking for rb_hash_dup()... yes
checking for rb_intern3()... yes
checking for rb_big_cmp()... yes
-----
Using mysql_config at /usr/local/bin/mysql_config
-----
checking for mysql.h... yes
checking for SSL_MODE_DISABLED in mysql.h... no
checking for MYSQL_OPT_SSL_ENFORCE in mysql.h... no
checking for errmsg.h... yes
checking for mysqld_error.h... yes
-----
Don't know how to set rpath on your system, if MySQL libraries are not in path mysql2 may not load
-----
-----
Setting libpath to /usr/local/Cellar/mysql/5.6.26/lib
-----
creating Makefile
To see why this extension failed to compile, please check the mkmf.log which can be found here:
/workdir/vendor/bundle/ruby/2.3.0/extensions/x86_64-darwin-15/2.3.0-static/mysql2-0.4.5/mkmf.log
current directory: /workdir/vendor/bundle/ruby/2.3.0/gems/mysql2-0.4.5/ext/mysql2
make "DESTDIR=" clean
current directory: /workdir/vendor/bundle/ruby/2.3.0/gems/mysql2-0.4.5/ext/mysql2
make "DESTDIR="
compiling client.c
compiling infile.c
compiling mysql2_ext.c
compiling result.c
compiling statement.c
linking shared-object mysql2/mysql2.bundle
ld: library not found for -lssl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mysql2.bundle] Error 1
make failed, exit code 2
Gem files will remain installed in /workdir/vendor/bundle/ruby/2.3.0/gems/mysql2-0.4.5 for inspection.
Results logged to /workdir/vendor/bundle/ruby/2.3.0/extensions/x86_64-darwin-15/2.3.0-static/mysql2-0.4.5/gem_make.out
Using puma 3.8.2
Using bundler 1.12.5
Using sass 3.4.23
Using tilt 2.0.7
Using turbolinks-source 5.0.0
Using tzinfo 1.2.3
Using nokogiri 1.7.1
Using rack-test 0.6.3
Using sprockets 3.7.1
Using websocket-driver 0.6.5
Using mime-types 3.1
Using coffee-script 2.4.1
Using uglifier 3.2.0
Using rb-inotify 0.9.8
An error occurred while installing mysql2 (0.4.5), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.4.5'` succeeds before bundling.
```
解決方法
$ xcode-select --install
xcodeのコマンドラインツールがインストールしてないだけだった。
xcodeのversionあげた場合とか気をつけたほうがよい。
mac 10.12.3
ruby 2.3.1p112
Bundler version 1.12.5
```
Installing mysql2 0.4.5 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
current directory: /workdir/vendor/bundle/ruby/2.3.0/gems/mysql2-0.4.5/ext/mysql2
/Users/yoshifuji.hiroki/.rbenv/versions/2.3.1/bin/ruby -r ./siteconf20170415-12041-6ex24h.rb extconf.rb
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for ruby/thread.h... yes
checking for rb_thread_call_without_gvl() in ruby/thread.h... yes
checking for rb_thread_blocking_region()... no
checking for rb_wait_for_single_fd()... yes
checking for rb_hash_dup()... yes
checking for rb_intern3()... yes
checking for rb_big_cmp()... yes
-----
Using mysql_config at /usr/local/bin/mysql_config
-----
checking for mysql.h... yes
checking for SSL_MODE_DISABLED in mysql.h... no
checking for MYSQL_OPT_SSL_ENFORCE in mysql.h... no
checking for errmsg.h... yes
checking for mysqld_error.h... yes
-----
Don't know how to set rpath on your system, if MySQL libraries are not in path mysql2 may not load
-----
-----
Setting libpath to /usr/local/Cellar/mysql/5.6.26/lib
-----
creating Makefile
To see why this extension failed to compile, please check the mkmf.log which can be found here:
/workdir/vendor/bundle/ruby/2.3.0/extensions/x86_64-darwin-15/2.3.0-static/mysql2-0.4.5/mkmf.log
current directory: /workdir/vendor/bundle/ruby/2.3.0/gems/mysql2-0.4.5/ext/mysql2
make "DESTDIR=" clean
current directory: /workdir/vendor/bundle/ruby/2.3.0/gems/mysql2-0.4.5/ext/mysql2
make "DESTDIR="
compiling client.c
compiling infile.c
compiling mysql2_ext.c
compiling result.c
compiling statement.c
linking shared-object mysql2/mysql2.bundle
ld: library not found for -lssl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mysql2.bundle] Error 1
make failed, exit code 2
Gem files will remain installed in /workdir/vendor/bundle/ruby/2.3.0/gems/mysql2-0.4.5 for inspection.
Results logged to /workdir/vendor/bundle/ruby/2.3.0/extensions/x86_64-darwin-15/2.3.0-static/mysql2-0.4.5/gem_make.out
Using puma 3.8.2
Using bundler 1.12.5
Using sass 3.4.23
Using tilt 2.0.7
Using turbolinks-source 5.0.0
Using tzinfo 1.2.3
Using nokogiri 1.7.1
Using rack-test 0.6.3
Using sprockets 3.7.1
Using websocket-driver 0.6.5
Using mime-types 3.1
Using coffee-script 2.4.1
Using uglifier 3.2.0
Using rb-inotify 0.9.8
An error occurred while installing mysql2 (0.4.5), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.4.5'` succeeds before bundling.
```
解決方法
$ xcode-select --install
xcodeのコマンドラインツールがインストールしてないだけだった。
xcodeのversionあげた場合とか気をつけたほうがよい。
Railsの開発環境をDockerで作った時にハマったところ Rails, Nginx, Redis, MySQL
docker-composeを使って構築する時にハマったところ
ホストマシン(Macのローカル環境)とDockerコンテナとのやりとり
- ports ( -pオプション)でやりとりできるのはホストマシンのPortをマッピングする
なので
Dockerコンテナ同士のPortをマッピングしているわけではない
例えば、MySQLのコンテナ(Port:3306)にホストマシンからアクセスできるからといって、Railsを動かしているコンテナからアクセスはできない。
ホストマシン : mysql -h127.0.0.1 -uroot => OK
Railsコンテナ : mysql -h 127.0.0.1 -uroot => NG
linksを使って指定すれば、名前解決できるのでそれを使ってアクセスする
links:
- mysql
Railsコンテナ : mysql -h mysql -uroot => OK
RailsアプリのMySQLへの接続、Redisへの接続などはホスト名の指定方法を変更する必要がある!!
修正前
database.yml
host: localhost
development.rb
config.session_store :redis_store,
servers: 'redis://localhost:6379/0',
修正後
database.yml
url: mysql2://root:@mysql:3306
development.rb
config.session_store :redis_store,
servers: 'redis://redis:6379/0',
Railsアプリの共有方法
ディレクトリ構成を以下のようにしていた
任意のディレクトリ/RailsApp/
Gemfile
app
bin
config
db
docker-compose/
nginx/
Dockerfile
app.conf
app/
Dockerfile
docker-compose.yml
そうすると、appのDockerfileに書くRailsAppのパスが親のパスになるため、Permissionエラーとなる
COPY .. /app
Ruby on Raileのユニットテスト
Ruby on Railsのユニットテストについて勉強
テストの種類とサポートツール
ユニットテストサポート
- Test::Unit
- Rails標準のフレームワーク - 可読性の欠ける
- RSpec
- 可読性に優れている - Test::Unitとは異なる独特の記法
- Shoulde
- Test::Utilからの移行が容易 - 機能面で、RSpecに及ばない
エンドツーエンドテスト
- Request Spece
- rspec-rails 標準
- Steak
- 受け入れテストを記述できる
- Cucumber
- 自然言語的な記述でテストシナリオの記述が可能
RSpecによるユニットテスト
- リポジトリの作成 Test::Utilを組み込まない
- reils new [APP_PATH] -T
- rspec-rails のインストール
- Gemfileに追記
group :development, :test do
gem "rspec", "2.4.0"
gem "rspec-rails", "2.4.1"
end
- ライブラリのインストール
bundle install
- RSpecの設定ファイル生成
rails generate rspec:install
モデルのテスト
- モデル生成
reils generate model article title:string body:text
- マイグレーションスクリプト実行
rake db:migrate
- ※ undefined method `prerequisites' for nil:NilClass
エラーの場合、rspecのバージョンを消してbundle updateを実行
group :development, :test do
gem "rspec"
gem "rspec-rails"
end
- データロード
rake db:test:load
- テストの実行
- spec/models/article_spec.rb
require 'spec_helper'
describe Article do
context "title and body init" do
before do
@article = Article.new(
:title => "first blog",
:body => "first body"
)
end
it "title check" do
@article.title.should == "first blog"
end
it "body check" do
@article.body.should == "first body"
end
end
end
- コマンド
bundle exec rspec spec/models/article_spec.rb
- 出力の変更
- オプション-f dをつける
- .rspecで設定可能
bundle exec rspec -f d spec/models/article_spec.rb
- バリデーションのテスト
- app/models/article.rbにバリデーションを追加
class Article > ActiveRecord::Base
attr_accessible :body, :title
validates :title, :presence => true
end
- テストの追加
context "not title" do
before do
@article = Article.new
end
it { @article.should_not be_valid } end
context "exist title" do
before do
@article = Article.new(:title => "first blog")
end
it { @article.should be_valid }
end
コントローラのテスト
- コントローラの生成
- rails g controller articles index
- コントローラで使うメソッド達
- get, post, put, delete => HTTPリクエスト発行
- response => HTTPレスポンスの取得
- assigns => コントローラのいんスタン変数を取得
- flash, session, cookie => 各項目の取得
- 一覧表示機能のテスト
- spec/controllers/articles_controller_spec.rbの実装
require 'spec_helper'
describe ArticlesController do
describe "GET 'index'" do
before do
@article1 = Article.create(
:title => "Article 1",
:body => "Hello"
)
@article2 = Article.create(
:title => "Article 2",
:body => "World"
)
get 'index'
end
it "should be success" do
response.should be_success
end
it "all articles" do
assigns[:articles].should =~ [@article1, @article2]
end
end
end
- コントローラに一覧取得処理を追加
class ArticlesController < ApplicationController
def index
@articles = Article.all
end
end
ビューのテスト
- 一覧画面のテスト
require 'spec_helper'
describe "articles/index.html.erb" do
before do
assign(
:articles, [
Article.create(
:title => "first blog",
:body => "content"
)
]
)
render
end
it "title view" do
rendered.should =~ /first blog/
end
it "body view" do
rendered.should =~ /content/
end
end
- viewのテストで使うメソッド
- assign => ビューのインスタンス変数に値を設定する
- render => ビューのレンダリングを行う
- rendered => レンダリングした結果を返す
- Viewの実装
<h1>Articles#index</h1>
<% @articles.each do |article| %>
<h2><%= article.title %></h2>
<div><%= article.body %></div>
<% end %>
ヘルパーのテスト
- 登校日を表示するヘルパー
require 'spec_helper'
describe ArticlesHelper do
describe "#posted_on" do
before do
@now = Time.now
@article = Article.create(
:title => 'first blog',
:body => 'content'
)
end
it {
helper.posted_on(@article).should == "#{@now.year}/#{@now.month}/#{@now.day}"
}
end
end
- ヘルパーのテストで使うメソッド
- assign => インスタンス変数をセットする
- helper => 対象ページのヘルパーをインクルードしたオブジェクトにアクセス
- ヘルパーの実装
module ArticlesHelper
def posted_on(article)
time = article.created_at
"#{time.year}/#{time.month}/#{time.day}"
end
end
Rails入門(チュートリアル)
ruby on rails の勉強がてら、本家のチュートリアルを一通り実行してみる
3.2 プロジェクトの作成
$ rails new blog
- ヘルプが見れる(rails new -h)
3.3.1 DBの内容
config/database.yml
- 各環境の設定内容が記述されている
- デフォルトではsqlite3
3.4 DBの生成
$ rake db:create
4.1 サーバ起動
$ rails server
- http://localhost:3000 にアクセスするとwelcomeページが表示される
- 終了はCtrl+c
4.2 お決まりのHello world
- コントローラの生成
$ rails generate controller home index
- 生成されるコントローラは app/controllers/home_controller.rb
- view(html)を修正app/views/home/index.html.erb
<h1>Hello, Rails!</h1>
4.3 アプリケーションのHomeページを設定
- 必ずpublic/index.html が表示されてしまうため削除
$ rm public/index.html
- ディスパッチの設定を追加
- ファイル: config/routes.rb
Blog::Application.routes.draw do
#...
# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
root :to => "home#index"
- その後再度 http://localhost:3000 にアクセスすると、先ほどのテンプレートが表示される
6 リソースの作成(MVCを作る)
- scaffold を指定すると、model, view, controllerを一括で作成してくれる
$ rails generate scaffold Post name:string title:string content:text
6.1 マイグレーション
- テーブルの作成や、変更などを管理する仕組み
- ファイル :db/migrate/20120508162203_create_posts.rb
- マイグレーションスクリプト実行
$ rake db:migrate
- productionで作成する場合はオプションをつける rake db:migrate RAILS_ENV=production
6.2 Topページにリンクを追加
- ファイル : app/views/home/index.html.erb
<h1>Hello, Rails!</h1>
<%= link_to "My Blog", posts_path %>
- link_to [タイトル], 遷移先?
6.4 モデルの確認
- ファイル : app/models/post.rb
6.5 バリデータの追加
- ファイル : app/models/post.rb
class Post < ActiveRecord::Base
attr_accessible :content, :name, :title
validates :name, :presence => true
validates :title, :presence => true,
:length => { :minimum => 5 }
end
- attr_accessibleは変更可能なパラメータ
- presence は必須、lengthは長さを指定
6.6 コンソールを使ってみよう
$ rails console
>> p = Post.new(:content => "A new post")
=> #<Post id: nil, name: nil, title: nil,
content: "A new post", created_at: nil,
updated_at: nil>
>> p.save
=> false
>> p.errors.full_messages
=> ["Name can't be blank", "Title can't be blank", "Title is too short (minimum is 5 characters)"]
- バリデータが効いているのがわかる
6.7 一覧ページ
- ファイル : app/controllers/posts_controller.rb
def index
@posts = Post.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @posts }
end
end
- @postsに投稿内容をすべて取得する
- html, jsonに対応
- http://localhost:3000/posts.html
- http://localhost:3000/posts.json
- テンプレート : app/views/posts/index.html.erb
- html エスケープしない場合、
- rails 2 だと <%=h post.name %>
- rails 3 では <%= raw post.name %>
6.8 カスタムレイアウト
- フッターやヘッダーを定義
- ファイル : app/views/layouts/application.html.erb
- 各コントローラ毎に指定する場合
- app/views/layouts/posts.html.erb
6.9 新規作成ページ
- ファイル : app/controllers/posts_controller.rb
def new
@post = Post.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @post }
end
end
- テンプレート : app/views/posts/new.html.erb
<h1>New post</h1>
<%= render 'form' %>
<%= link_to 'Back', posts_path %>
- render 'form'を指定することで、app/views/posts/_form.html.erb の内容を出力する
- 投稿処理
def create
@post = Post.new(params[:post])
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render json: @post, status: :created, location: @post }
else
format.html { render action: "new" }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
6.10 詳細表示
- http://localhost:3000/posts/1 にアクセスすると /posts/(id) として、:idに取得可能
def show
@post = Post.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render :json => @post }
end
end
- テンプレート : app/views/posts/show.html.erb
6.11 編集ページ
- 詳細表示と違い、jsonでのレスポンスがないためシンプル
def edit
@post = Post.find(params[:id])
end
- 更新処理
def update
@post = Post.find(params[:id])
respond_to do |format|
if @post.update_attributes(params[:post])
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
- update_attributesで必要なパラメータを更新
6.12 削除
- ファイル : app/controllers/posts_controller.rb
def destroy
@post = Post.find(params[:id])
@post.destroy
respond_to do |format|
format.html { redirect_to posts_url }
format.json { head :no_content }
end
end
7.1 モデルの追加
- テーブル間のリレーションシップを指定 post:references
$ rails generate model Comment commenter:string body:text post:references
- モデルクラスにも定義されている
- ファイル : app/models/comment.rb
class Comment < ActiveRecord::Base
belongs_to :post
attr_accessible :body, :commenter
end
- 再度マイグレーションスクリプトが生成されているて、それを反映
- ファイル : db/migrate/20120508170658_create_comments.rb
$ rake db:migrate
7.2 関連づけモデル
- 1投稿に付き複数のコメントが存在するためPostのmodelを修正
- ファイル :app/models/post.rb
class Post < ActiveRecord::Base
attr_accessible :content, :name, :title
validates :name, :presence => true
validates :title, :presence => true,
:length => { :minimum => 5 }
has_many :comments
end
7.3 ルート(ディスパッチ)にコメントを追加
- ファイル : config/routes.rb
- posts部分を修正
resources :posts do
resources :comments
end
7.4 コントローラの追加
$ rails generate controller Comments
- 上記エラー出る場合、ルートファイルで追加した部分をコメントアウトする
- Postのテンプレートにcomment投稿を追加
- ファイル : app/views/posts/show.html.erb
<p id="notice"><%= notice %></p>
<p>
<b>Name:</b>
<%= @post.name %>
</p>
<p>
<b>Title:</b>
<%= @post.title %>
</p>
<p>
<b>Content:</b>
<%= @post.content %>
</p>
<h2>Add a comment:</h2>
<%= form_for([@post, @post.comments.build]) do |f| %>
<div class="field">
<%= f.label :commenter %><br />
<%= f.text_field :commenter %>
</div>
<div class="field">
<%= f.label :body %><br />
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<%= link_to 'Edit', edit_post_path(@post) %> |
<%= link_to 'Back', posts_path %>
- コメントコントローラに処理を追記
- ファイル : app/controllers/comments_controller.rb
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.create(params[:comment])
redirect_to post_path(@post)
end
- 詳細画面でコメントを閲覧できるように修正
- ファイル : app/views/posts/show.html.erb
<h2>Comments</h2>
<% @post.comments.each do |comment| %>
<p>
<b>Commenter:</b>
<%= comment.commenter %>
</p>
<p>
<b>Comment:</b>
<%= comment.body %>
</p>
<% end %>
8.リファクタリング
- テンプレートの共通部分を外だししてみる
8.1 コレクション
- コメント部分のテンプレを外だし
- ファイル : app/views/comments/_comment.html.erb
<p>
<b>Commenter:</b>
<%= comment.commenter %>
</p>
<p>
<b>Comment:</b>
<%= comment.body %>
</p>
- 参照部分の修正 app/views/posts/show.html.erbを修正 (まだ納得は行かない><)
- 修正前
<% @post.comments.each do |comment| %>
<p>
<b>Commenter:</b>
<%= comment.commenter %>
</p>
<p>
<b>Comment:</b>
<%= comment.body %>
</p>
<% end %>
- 修正後
<%= render @post.comments %>
8.2 Form部分の外だし
- ファイル : app/views/comments/_form.html.erb
<%= form_for([@post, @post.comments.build]) do |f| %>
<div class="field">
<%= f.label :commenter %><br />
<%= f.text_field :commenter %>
</div>
<div class="field">
<%= f.label :body %><br />
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
- 参照部分の修正 : app/views/posts/show.html.erb
- 今回は単純に置き換える
<%= render "comments/form" %>
9 コメントの削除
- リンクの追加 : app/views/comments/_comment.html.erb
<p>
<%= link_to 'Destroy Comment', [comment.post, comment],
:confirm => 'Are you sure?',
:method => :delete %>
</p>
- コントローラに追加 : app/controllers/comments_controller.rb
def destroy
@post = Post.find(params[:post_id])
@comment = @post.comments.find(params[:id])
@comment.destroy
redirect_to post_path(@post)
end
9.1 関連データの削除も対応
- 親の投稿データが削除された場合、一緒に削除
- ファイル : app/models/post.rb
class Post < ActiveRecord::Base
attr_accessible :content, :name, :title
validates :name, :presence => true
validates :title, :presence => true,
:length => { :minimum => 5 }
has_many :comments, :dependent => :destroy
end
10 セキュリティー
- Basic認証
- ファイル : app/controllers/posts_controller.rb
class PostsController < ApplicationController
http_basic_authenticate_with :name => "dhh", :password => "secret", :except => [:index, :show]
# GET /posts
# GET /posts.json
def index
@posts = Post.all
- :exceptで対象外を指定
- :only => :destroy を指定すると、削除のみ認証する
11 複数モデル
- タグモデルの生成
$ rails generate model tag name:string post:references
- マイグレーション実行
$ rake db:migrate
- モデルに関連づけを行う
- ファイル : app/models/post.rb
has_many :tag
accepts_nested_attributes_for :tags, :allow_destroy => :true,
:reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
- accepts_nested_attributes_forは親子関係の定義?
- allow_destroyはネストした属性にチェックボックスを表示
- reject_if は禁止条件を設定
- フォームテンプレートにタグを追加
- ファイル : views/posts/_form.html.erb
<h2>Tags</h2>
<%= render :partial => 'tags/form',
:locals => {:form => post_form} %>
- タグ用のフォームテンプレート
- ファイル : app/views/tags/_form.html.erb
<%= form.fields_for :tags do |tag_form| %>
<div class="field">
<%= tag_form.label :name, 'Tag:' %>
<%= tag_form.text_field :name %>
</div>
<% unless tag_form.object.nil? || tag_form.object.new_record? %>
<div class="field">
<%= tag_form.label :_destroy, 'Remove:' %>
<%= tag_form.check_box :_destroy %>
</div>
<% end %>
<% end %>
- 投稿テンプレートにタグを追加
- ファイル : app/views/posts/show.html.erb
12 View Helpers
- ファイル : app/helpers/posts_helper.rb
- 使ってみる : app/views/posts/show.html.erb
- 変更前
- 変更後
<p>
<b>Tags:</b>
<%= join_tags(@post) %>
</p>
はじめてのRuby
RVM(Ruby Version Manager)
- RVMとは複数バージョンのRubyを使えるルーツ
rvmのインストール
$ curl -L get.rvm.io | bash -s stable.bashrcに追加
source ~/.rvm/scripts/rvm
Rudyのインストール
rudyのインストール可能なバージョンを確認$ rvm list known
rudyのインストール
$ rvm install 1.9.2
The provided compiler '/usr/bin/gcc' is LLVM based, it is not yet fully supported by ruby and gems, please read `rvm requirements`
- Mac OSX Lionではインストール時にエラーが出たので、下記を実行
- export CC=/usr/bin/gcc-4.2
インストールバージョンの確認
$ rvm list
最新版にするには
$ rvm get head
$ rvm reload
Rudy on Railsのインストール
Rudyのパッケージ管理ツール(gem)を使用してrailsをインストール
$ gem install railsアプリケーションの作成
プロジェクトの作成
$ rails new booksパッケージの管理
Bundlerは複数のPCで必要なGemパッケージをインストールする仕組みを提供してくれる- Gemfile : 必要なパッケージ情報
- Gemfile.lock : インストール済み情報
静的コンテンツ
PROJECT/publicMVC生成コマンド
コントローラの作成
$ rails generate controller [コントローラ名]DBの作成
$ rake db:create- config/database.yml の内容でDBを作成する
モデルの作成
$ rails generate model titleScaffold機能
- controller, model, viewをコマンド一つで実行してくれるマイグレーション
テーブルの作成や、変更の仕組みとしてマイグレーションと呼ばれる機能を提供マイグレーションスクリプト実行
$ rake db:migrateレスポンス関連
レスポンス形式の変更
respond_to do |format|format.html
format.json {render :json => オブジェクト}
format.xml {render :xml => オブジェクト}
end
リダイレクト
render_to :action => 'アクション名'
登録:
投稿 (Atom)
ReactNativeでAndroid対応する話
前提 ReactNativeでiOS版のアプリをリリースしていて、Android版をリリースする話 トラブルシューティング Build.VERSION_CODES.Q が存在しないエラー compileSdkVersionを29以上にすると解決 メモリー足りないエラー Execu...
-
公式のドキュメント https://docs.mitmproxy.org/stable/ # install ## mac ``` $ brew install mitmproxy ``` ## iPhone ### 証明書 セキュアな通信を傍受するために、プロファイ...
-
ナビゲーションコントローラを滑らかに遷移させる ナビゲーションコントローラの画面遷移をカスタマイズする 主要なクラス UIViewControllerAnimatedTransitioning 具体的なアニメーションを定義するクラス。実行時間やviewの動きな...
-
前提 ReactNativeでiOS版のアプリをリリースしていて、Android版をリリースする話 トラブルシューティング Build.VERSION_CODES.Q が存在しないエラー compileSdkVersionを29以上にすると解決 メモリー足りないエラー Execu...