ihatov08 blog

プログラミング初心者だけどRailsとSwiftマスターします

hostファイルを編集して、独自のホスト名を設定する方法

hostファイルを編集して、独自のホスト名を設定する方法

ホストOSで

$ cd
$ cd /private/etc
$ ls #lsコマンドでhostsファイルがあるか確認できる。
$ vi hosts #編集する

[i]を押してインサートモードに

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1       localhost
127.0.0.1       example.com hoge.example.com #今回はこの行を追加
255.255.255.255 broadcasthost
::1             localhost

[esc]キーでインサートモード終了 [:wq]で保存して終了

これでホストOSでlocalhost:3000以外にも、http://example.com:4000http://hoge.example.com:4000にアクセスしてもホストが通る

railsでmysqlのエラーが起きた時の対処法

bin/rake db:create

エラーが起きる

/home/vagrant/.gem/ruby/2.1.0/gems/activerecord-4.1.0/lib/active_record/connection_adapters/connection_specification.rb:190:in `rescue in spec': Specified 'mysql2' for database adapter, but the gem is not loaded. Add `gem 'mysql2'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord). (Gem::LoadError)

gemfileでmysqlのバージョンを指定する。 mysqlのバージョンは指定しておいた方がいい。

gemfile
gem 'mysql2', '~> 0.3.13'
bin/bundle update mysql2

もう一度データベースを作成する。

bin/rake db:create

railsのdeviseでregistrations/newでpassword__confirmationはいらなかった

railsのdeviseを使う際に、registration/new.html.erbでpassword_confirmationカラムは入力必須で、このカラムをなくすには何かコードを書くか削除する必要があると思っていた。 code4startupでviewから単純に削除していたので、webricksで挙動を試してみたら、viewから削除するだけでログインできた。 こんな簡単にできるのかとびっくりした。 つまりパスワードを2回入力することなく、(確認を入力する必要なく)ログインできる。 deviseすごい。

Railsでリセットボタンの実装方法

リセットボタン<input type="reset" ?>と普通のボタン<input type="button" ?>を作るためのメソッドRailsには存在しない。一般的なタグを作るメソッドtagで次のように記述するか、<input type="reset" value="リセット" ?>のようにHTMLのタグを直接記述する。

<%= tag :input, :type => 'reset', :value => 'リセット' %>

<%= f.button "リセット", type: :reset %>
#=><button name="button" type="reset">リセット</button>