deviseを使ってログイン処理をしてみる。
ログイン処理を作るのでdeviseのメモ。
Deep valley まとめ版 - RailsでDeviseを使ってみたを参考にやってみる。
環境: Ubuntu11.10, RVM1.13.4, Rails3.2.3
1. deviseをインストールする
% rvmsudo gem install devise
2. deviseをrailsで使えるようにする。
% rails generate devise:install
このコマンドを実行すると、設定用ファイルが作成される。
さらに、以下のメッセージ。
===============================================================================
Some setup you must do manually if you haven't yet:
1. Ensure you have defined default url options in your environments files. Here
is an example of default_url_options appropriate for a development environment
in config/environments/development.rb:config.action_mailer.default_url_options = { :host => 'localhost:3000' }
In production, :host should be set to the actual host of your application.
2. Ensure you have defined root_url to *something* in your config/routes.rb.
For example:root :to => "home#index"
3. Ensure you have flash messages in app/views/layouts/application.html.erb.
For example:
<%= notice %>
<%= alert %>
4. If you are deploying Rails 3.1 on Heroku, you may want to set:
config.assets.initialize_on_precompile = false
On config/application.rb forcing your application to not access the DB
or load models when precompiling your assets.===============================================================================
どうやらいくつか設定が必要なようだ。
3. 手動でdevise用の設定を行う。
- development.rbに追加。
config/environmets/development.rbに以下の文を追加。
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
- routes.rbに追加。
deviseではログイン処理を行うと、rootに設定されたページにリダイレクトするようになっている。
config/routes.rbを編集する。なお、homeコントローラとindexアクションが定義されているものとする。
root :to => "home#index"
- application.html.erbに追加。
app/view/layouts/application.html.erbに以下の文を追加。
<%= notice %>
<%= alert %>
- 認証用のモデルを作成する。
% rails generate devise user
4. ログイン画面へのリンクを作成する。
app/view/home/index.html.erbに以下の文を追加。
<% if user_signed_in? %>
<%= link_to "ログアウト", destroy_user_session_path, method: :delete %>
<% else %>
<%= link_to "ログイン", new_user_session_path %>
<% end %>
これでログイン画面へのリンクが貼られる。
☆私がはまったところ
先述のように、deviseではログイン処理を行うと、rootに設定されたページにリダイレクトするようになっている。しかし、私の場合はrootの設定を行っていてもそこにリダイレクトされず、localhost:3000にリダイレクトされていた。
調べてみたところ、ルーティングの設定を行っても静的なHTMLページの方が優先されて表示されてしまうとのこと。今回のケースでは「public/index.html」が存在しており、こちらが表示されていた。リネームしたらうまくいった。
UbuntuでEclipseを日本語で導入するメモ
環境: Ubuntu11.10 in VMware Player 4.0.2 on Windows 7 Professional
参考: Eclipseを日本語化するには - Ubuntu Japanese Wiki
1. Pleiades パッケージをインストールする。
sudo aptitude install pleiades
このとき、依存関係でEclipse もインストールされる。
2. 設定ファイルを更新する。
/etc/eclipse.ini の
-vmargs よりも下に以下を追加。
-javaagent:/usr/lib/eclipse/plugins/jp.sourceforge.mergedoc.pleiades/pleiades.jar
ただし、/etc/eclipse.ini はroot 権限が必要なので、以下のコマンドでemacs を起動。
日本語でeclipse が起動した。
ちなみに、私はインストールと編集にそれぞれaptitude とemacs を使ったが、apt-get とgedit で代用できる。gedit で/etc/eclipse.ini を編集するときは、
gksu gedit /etc/eclipse.ini
でgedit を起動する。
今回の教訓。
しばらく使っていないソフトはアップデートしましょう。
gemにライブラリがインストールされないよー。
……という問題。
rmagick2.13.1をgemにインストールしようとして、
sudo gem install rmagick
として、エラーがなく終了したが、
gem list
で確認してもインストールされていない。
原因はコマンドの間違い。
rvmを使用している場合、gemにライブラリをインストールするコマンドは
rvmsudo gem install rmagick
だった。
何やってるんだろ私。
Windows上にPostgreSQLを導入する@zip archive
PostgreSQL導入のメモ。
OS: Windows 7 Proffesional 64bit
PostgreSQLのバージョン: 9.1.3
1. PostgreSQLをダウンロードする
PostgreSQL: WelcomeよりPostgreSQLの最新版をダウンロードする。
Downloadの項目をクリックし、Binary packagesのリンクをOSに応じて進む。
One click installerがあるが、私の場合実行しても動作が停止してしまったので、zip archiveを用いてインストールする。
クリックした先でOSに対応したzipファイルをダウンロードする。私の場合は"postgresql-9.1.3-1-windows-x64-binaries.zip"だった。
以降、Installing PostgreSQL 9.1 to Windows 7 from the Binary Zip Distribution | Petri Kainulainenを参考に行う。あ、英語サイトなので私が誤訳して違うことやってるかも。
2. 解凍する
ダウンロードしたzipファイルを解凍する。"pgsql"というディレクトリができるはず。
3. パスを通す
解凍したディレクトリ内にあるbinディレクトリにパスを通す。
やり方は割愛。
4. ディレクトリを作る
データ用のディレクトリとログ用のディレクトリを作る。"pgsql"内に"data"と"log"といったディレクトリを作る。
5. PostgreSQLの設定を行う
コマンドライン上で、以下のコマンドを入力する。
> cd pgsqlの場所
> initdb -U postgres -A password -E utf8 -W -D pgsql\data
これで基本的な設定を行える。オプションの意味は以下。
- U : 作成するデータベースクラスタのスーパーユーザ名
- A : 認証方法の指定。passwordはパスワードを用いた認証を行うの意
- E : 文字コードの指定
- D : データベースクラスタを格納するディレクトリ(相対パス)。未指定の場合は環境変数PGDATAが指定される。
これで設定完了。
6. 動作テスト
サーバとして動くかテストする。
> pg_ctl -D pgsql\data -L pgsql\log\logfile start
オプションの意味は以下。
これで動いているはず。
確認してみる。
> pg_ctl -D pgsql\data status
停止してみる。
> pg_ctl -D pgsql\data stop
こんな出力になればいいみたい。
Ruby on RailsをUbuntuに導入する
Ubuntu上でRuby on Railsを参考にRuby on Railsを導入する。
前提として、UbuntuにRubyを導入する - みさとのPCめもの続き。
1. gemを使えるようにする。
% sudo apt-get install Rubygems
これでgemコマンドを使えるようにする。
2. gemsetを作成する。
% rvm gemset list
% rvm gemset create rails3
% rvm gemset use rails3
3. Railsをインストールする。
% rvmsudo gem install rails
4. Ubuntu上でRuby on Railsからの引用
Railsで入れるべきgemライブラリーをいちいちインストールするのは面倒なので、bundleを利用してまとめてインストールする。まずは新しいプロジェクトを作成する
% rails new demo
% cd demo
% vi Gemfile
Gemfileに以下を付け加える。
gem 'execjs'
gem 'therubyracer'
必要なgemライブラリーを追加する。
% bundle install
―――― 以上引用 ――――
と、ここでエラー。
Installing therubyracer (0.10.1) with native extensions
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension./usr/bin/ruby1.8 extconf.rb
Invalid gemspec in [/var/lib/gems/1.8/specifications/mail-2.4.4.gemspec]: invalid date format in specification: "2012-03-14 00:00:00.000000000Z"
Invalid gemspec in [/var/lib/gems/1.8/specifications/mime-types-1.18.gemspec]: invalid date format in specification: "2012-03-21 00:00:00.000000000Z"
Invalid gemspec in [/var/lib/gems/1.8/specifications/libv8-3.3.10.4-x86-linux.gemspec]: invalid date format in specification: "2011-11-15 00:00:00.000000000Z"
Invalid gemspec in [/var/lib/gems/1.8/specifications/jquery-rails-2.0.2.gemspec]: invalid date format in specification: "2012-04-03 00:00:00.000000000Z"
Invalid gemspec in [/var/lib/gems/1.8/specifications/sass-rails-3.2.5.gemspec]: invalid date format in specification: "2012-03-19 00:00:00.000000000Z"
Invalid gemspec in [/var/lib/gems/1.8/specifications/tilt-1.3.3.gemspec]: invalid date format in specification: "2011-08-25 00:00:00.000000000Z"extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers. Check the mkmf.log file for more
details. You may need configuration options.Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/usr/bin/ruby1.8
/usr/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require': no such file to load -- libv8 (LoadError)
from /usr/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb:36:in `require'
from extconf.rb:7
Gem files will remain installed in /home/handa/.bundler/tmp/20818/gems/therubyracer-0.10.1 for inspection.
Results logged to /home/handa/.bundler/tmp/20818/gems/therubyracer-0.10.1/ext/v8/gem_make.out
An error occured while installing therubyracer (0.10.1), and Bundler cannot continue.
Make sure that `gem install therubyracer -v '0.10.1'` succeeds before bundling.
libv8がインストールされていないとか、"gem install therubyracer -v '0.10.1'"を試してみてと言われているが、今回のケースの場合日付の指定が有効でなかったことが問題だった。(libv8についてはちゃんとインストールされている。)
Ubuntu + Ruby環境をつくる | 崖の上のプログラマー日記 を参考に、以下のコマンドを実行。
% sudo find /var/lib/gems/1.8/specifications/ -name “*.gemspec” -exec sed -i ‘s/ 00:00:00.000000000Z//’ {} \;
日付の形式を直します。直っているか確認するために、以下のコマンド。
% grep "000000000Z" /var/lib/gems/1.8/specifications/*.gemspec
検出されなかったらOK。検出されたら、手動で削除。場合によってはスーパーユーザである必要性がある。
もう一回
% bundle install
うまくいった。
5. Ubuntu上でRuby on Railsからの引用
scaffoldしてrailsがちゃんと動くかを確認する。
% rails generate scaffold person name:string age:integer
% rake db:migrate
% rails server
http://localhost:3000/people で追加、削除、編集ができたら成功。
5/9追記
本手順における手順4において
% bundle install
Ruby1.9.3がインストールできない件
UbuntuにRubyを導入する - みさとのPCめもでうまくいかなかった部分の解説。
RVMをインストールしていきなりRuby1.9.3をインストールしようとすると、こんなエラーが出た。
Error running 'make ', please read /usr/local/rvm/log/ruby-1.9.3-head/make.log
There has been an error while running make. Halting the installation.
なんかインストールできなかったみたい。読めといわれたのでログを読むよ。
ログの抜粋
executable host ruby is required. use --with-baseruby option.
make: *** [miniprelude.c] エラー 1
……えーと。Rubyを使うためのRubyが必要、と。
解決方法はUbuntuにRubyを導入する - みさとのPCめもに書いた。
UbuntuにRubyを導入する
環境: Ubuntu11.10 in VMware Player 4.0.2 on Windows 7 Professional
とりあえずRubyはバージョンがいっぱいあるのでバージョン管理ソフトのRVM を使用する。
RVMについては以下のURLを参考に。
Ruby Freaks Lounge:第39回 RVM(Ruby Version Manager)による環境構築|gihyo.jp … 技術評論社
1. 必要なソフトをインストールする。
必要なプログラムを用意するよ。aptitudeとcURLとGit。
aptitudeについては以下を参照。
aptitude - Wikipedia
cURLについては以下を参照。
cURL - Wikipedia
Gitについては以下を参照。
Git - Wikipedia
以下のコマンドを入力。
% sudo apt-get install aptitude
パスワードを聞かれたりするので答える。質問はとりあえずYesで。
これでaptitudeのインストールはできる。
次にcURLとGitをインストールする。
質問はやっぱりYesで。
これでcURLとGitもインストール完了。
2. RVMをインストールする。
% sudo bash < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)
を実行。
これで/usr/local/rvm下にRVMがインストールされる。
注意: dpkgではインストールされているか分からない。
最後に、
% sudo addgroup USERNAME rvm
でユーザをグループに追加する。
.bashrcに以下の内容を追記。
if [[ -s /usr/local/rvm/scripts/rvm ]] ; then source /usr/local/rvm/scripts/rvm ; fi
これで完了。
3. Rubyをインストールする準備をする。
まず、インストールに必要なものを調べる。
% rvm requirements
の出力結果のうち、
Additional Dependencies:
# For Ruby / Ruby HEAD (MRI, Rubinius, & REE), install the following:
ruby: /usr/bin/apt-get install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion
に書かれてるものを一通りインストールする。
% sudo aptitude build-essential openssl (中略) bison subversion
と、こんな表示が。
次に、Ruby1.8.7をインストールする。
というのは、いきなりRubyの最新安定版(書いた段階では1.9.3)をいきなりインストールするとエラーを吐かれてインストールできない。
詳細は別記事で書くよ。
% rvm install 1.8.7-head
なんとかできた。
% rvm list
でインストールされてるrubyのバージョンを確認できる。
4. Rubyの最新安定版をインストールする。
%rvm install 1.9.3-head
と。
終わったらインストールできてるか、確認。
参考URL:Ubuntu上でRuby on Rails