使用者認證
转自: [url]http://ihower.tw/rails3/auth.html[/url] 使用者認證 Quality, Speed or Cheap. Pick two. - Unknown Authentication: 使用 Devise devise是一套使用者認證(Authentication)套件。 編輯 Gemfile 加上 gem 'devise' 輸入bundle install安裝此套件 輸入rails g devise:install產生devise設定檔 編輯 config/environments/development.rb 和 production.rb 加入寄信時預設的網站網址: config.action_mailer.default_url_options = { :host => 'localhost:3000' } 確認 app/views/layouts/application.html.erb layout 中可以顯示 flash 訊息,例如
<%= notice %>
<%= alert %>
確認 routes.rb 中有設定網站首頁位置,例如 root :to => "welcome#index" 輸入rails g devise user產生 User model 及 Migration 如果需要E-mail驗證功能,可以編輯app/models/user.rb和migration將confirmable功能打開 輸入rails generate devise:views產生樣板,這會包括有註冊、登入、忘記密碼、Email等等頁面,放在app/views/devise目錄下。 輸入bundle exec rake db:migrate建立資料表 用法 在需要登入的 controller 加上before_filter :authenticate_user! 可以在 Layout 中加上登入登出選單 <% if current_user %> <%= link_to('登出', destroy_user_session_path, :method => :delete) %> | <%= link_to('修改密碼', edit_registration_path(:user)) %> <% else %> <%= link_to('註冊', new_registration_path(:user)) %> | <%= link_to('登入', new_session_path(:user)) %> <% end %> Authentication: 使用 Omniauth Omniauth Authentication: DIY Authentication in Rails 3.1 Authentication: SSO 可以採用CAS標準,推薦以下的Sinatra實作: rubycas-server ClassyCAS Authorization cancan declarative_authorization 更多 More