2012年11月23日金曜日

devise ユーザ情報編集画面

railsの説明をwebで見ていると、2つに1つは、ログイン出来たから終了、といった類の物、もしくは日本語化ができましたわ、といった物。

私は本業の傍ら、さすがに23時に仕事終わってからでは、酒を飲んで寝るだけだが、ソコソコの時間で帰宅出来た時に、酒を飲みながら開発をする「酔いどれ開発者」である。

教えを乞う側で、非常に生意気な発言になるが、酔いどれ開発者の私は、「ではインストールした後、どうしたんのですか?それで満足なのですか?」と問いかけたくなる。その続きは無い。有るのかも知れないが、公開していない。どちらか。

私は思うのだが、よっぽどの天才である限り、今、Railsなり何なり、具現化しようとしているアイディアは「在り来り」のものであるはずだ。よっぽどの天才であるスティーブですら、「未来を見て点と点を繋ぐことはできない、過去を振り返って点と点を繋ぐことはできる」私のは、アイディアどころか、ただITと古い文化を繋げるだけで、タイトルにも公開している。今まで開発されてこなかったアイディアであれば、これからも他人に開発される可能性は低いのだから、公開すればよいのに。その方が長続きする。

機能を試して満足されている方には、申し訳ない、と一言断らせて頂きます。
古い話になるが、学生時代の私がそうだったので。

1.さて、愚痴っぽくなったけれども、deviseでSign Inした後、登録内容を変更する場合の画面を表示させる。
application.html.rbに以下を追加。
<%= link_to "ユーザ情報の編集", edit_user_registration_path %>

2.表示名を編集する項目を追加。
view/devise/registrations/edit.html.erbに以下を追加
<div><%= f.label :name %><br />
 <%= f.email_field :name %></div>
お、間違えている、これで1時間ぐらいハマッてしまった。これだから酔いどれは。
 <%= f.text_field :name %></div>
うーん。本拠地のNOはイケてない。結局ControllerもOverwriteしないとダメだね。


2012年11月22日木曜日

deviseのSign in Sign out先

deviseのSign In、Sign Out先はroutes.rb
root :to =>
で設定した箇所だが、publicフォルダにあるindex.htmlが残っていると、有効にならないっぽいので、リネームすると有効になる(消す、でも良いが、システム開発で物理削除はなるべく避けて通ったほうが良い、若い頃から、いろいろ痛い目にあって、無駄なリカバリ時間を過ごした事多し)

2012年11月21日水曜日

deviseのviewを変更する

deviseはviewを意識しなくとも、そっけないinterfaceが出てくる。
但し、これから作るアプリはdeviseのデフォルトのviewでは項目が足りない。
それ以前に、まっさらなデザインがどうも。

さて、viewを変更しよう。

1.deviseのviewを出す。

$ rails generate devise:views

viewフォルダにdeviseフォルダが。

2.その前に日本語化
l18nを使ってdeviseを日本語化する。
2-1.https://github.com/plataformatec/devise/wiki/I18n
ここからJapaneseのdevise.ja.ymlをありがたく頂戴する。
2-2.config/localesに配置
2-3.config/application.rbに以下を追記
 config.i18n.default_locale = :ja
これでメッセージは日本語化される。見た目はviewチマチマなおさなあかん。

2012年11月18日日曜日

rails認証プラグイン:Devise導入:その1.5

去年、Deviseの記事を見返していると、どうもその1と、その2の間がぽっかり開いている。migreとかどうしたんよ、と怒れる諸兄の叱咤を受け、1.5を書こうと思う。

前回rails認証プラグイン:Devise導入:その1

1.ユーザモデルを作成
$ bundle exec rails g devise (ユーザ名)

2.作成されたmigreファイルを編集
ここで悩む。1年前にdevise導入した時は、

class DeviseCreateUser < ActiveRecord::Migration
  def change
    create_table(:user) do |t|
      t.database_authenticatable :null => false
      t.recoverable
      t.rememberable
      t.trackable

      # t.encryptable
      # t.confirmable
      # t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both
      # t.token_authenticatable


devise (2.1.2)は、

class DeviseCreateUsers < ActiveRecord::Migration
  def change
    create_table(:users) do |t|
      ## Database authenticatable
      t.string :email,              :null => false, :default => ""
      t.string :encrypted_password, :null => false, :default => ""

      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      t.integer  :sign_in_count, :default => 0
      t.datetime :current_sign_in_at
      t.datetime :last_sign_in_at
      t.string   :current_sign_in_ip
      t.string   :last_sign_in_ip

      ## Confirmable
      # t.string   :confirmation_token
      # t.datetime :confirmed_at
      # t.datetime :confirmation_sent_at
      # t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, :default => 0 # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at

      ## Token authenticatable
      # t.string :authentication_token

お、Database authenticatableや、Trackable等、モジュールに必要な項目が、予め明示的に展開されているじゃないですか。1年前は、ぱっと見て、どんなエンティティ構成になるか、不安でたまらないもんなぁ。少なくともdevise知らない人が、保守フェーズとかで見たら混乱するだろう、危ねぇ、と思っていたのですが、見事改善。
そして、ユーザ登録時のメール認証を使用したいので、confirmableのコメントアウトを解除。

3.modelにconfirmableを追加(メール認証有効化)
class user < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable

4.DBに反映する。
$ rake db:migrate

5.development.rbを編集
・以下のコメントアウトを追加

  # Don't care if the mailer can't send
  # config.action_mailer.raise_delivery_errors = false 

・SMTPサーバを、Gmailに乗る為、以下を追加する。

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    :address => 'smtp.gmail.com',
    :port => 587,
    :authentication => :plain,
    :user_name => 'hrendoh@gmail.com',
    :password => '****'
  }
6.試してみる
ん?Logには<p>Welcome yoshidaagri@gmail.com!</p>と書いていて、Confirm my accountのURLも発行済みなのだが、メールが来ない、うむむ、これは次回に調査するとして、ひとまずConfirm my accountのURLにブラウザでアクセス。
7.その前にapplication.html.erbのbody句に以下を追加
<div id="user_nav">
  <% if user_signed_in?%>
    Signed in as <%= current_user.email %>. Not you?    <%= link_to "Sign out", destroy_user_session_path %>
  <% else %>
    <%= link_to "Sign up", new_user_registration_path %> or
    <%= link_to "Sign in", new_user_session_path %>
  <% end %>
</div>


8.アクセスしてみる。
Signed in as xxxxxxxx@gmail.com. Not you? Sign out
Your account was successfully confirmed. You are now signed in.

出ました!

2012年11月10日土曜日

newでボケて

以下のエラーが出る。
Can't mass-assign protected attributes

これが出た時は、modelsフォルダにある、モデル名.rbの

attr_accessibleに、アプリから更新しても良いエンティティ項目をどんどん追加していけばよい、:user_idとか。

これを忘れて、なんじゃこりゃと、騒いている私は、初心者です。