restful_authentication是Rails的一个认证插件,基于REST方式,适合RESTful的geek
安装:
[code]
ruby script/plugin install http://svn.techno-weenie.net/projects/plugins/restful_authentication/
ruby script/generate authenticated users sessions
[/code]
可选择性的修改routes:
[code]
# routes.rb
ActionController::Routing::Routes.draw do |map|
map.home '', :controller => 'home', :action => 'index'
map.resources :users
map.resource :session
map.signup '/signup', :controller => 'users', :action => 'new'
map.login '/login', :controller => 'session', :action => 'new'
map.logout '/logout', :controller => 'session', :action => 'destroy'
end
[/code]
一些helper方法:
[code]
<!-- home/index.rhtml -->
<h1>Welcome</h1>
<% if logged_in? %>
<p><strong>You are logged in as <%=h current_user.login %></strong></p>
<p><%= link_to 'Logout', logout_path %></p>
<% else %>
<p><strong>You are currently not logged in.</strong></p>
<p>
<%= link_to 'Login', login_path %> or
<%= link_to 'Sign Up', signup_path %>
</p>
<% end %>
[/code]
安装:
[code]
ruby script/plugin install http://svn.techno-weenie.net/projects/plugins/restful_authentication/
ruby script/generate authenticated users sessions
[/code]
可选择性的修改routes:
[code]
# routes.rb
ActionController::Routing::Routes.draw do |map|
map.home '', :controller => 'home', :action => 'index'
map.resources :users
map.resource :session
map.signup '/signup', :controller => 'users', :action => 'new'
map.login '/login', :controller => 'session', :action => 'new'
map.logout '/logout', :controller => 'session', :action => 'destroy'
end
[/code]
一些helper方法:
[code]
<!-- home/index.rhtml -->
<h1>Welcome</h1>
<% if logged_in? %>
<p><strong>You are logged in as <%=h current_user.login %></strong></p>
<p><%= link_to 'Logout', logout_path %></p>
<% else %>
<p><strong>You are currently not logged in.</strong></p>
<p>
<%= link_to 'Login', login_path %> or
<%= link_to 'Sign Up', signup_path %>
</p>
<% end %>
[/code]