user can't delete self and flash_messages

This commit is contained in:
chris 2011-02-08 10:07:40 +08:00 committed by ihower
parent 318c264cae
commit e62df90117
9 changed files with 67 additions and 16 deletions

View File

@ -22,6 +22,7 @@ class Panel::UsersController < ApplicationController
def create def create
@user = User.new(params[:user]) @user = User.new(params[:user])
if @user.save if @user.save
flash[:notice] = t('panel.create_success_user')
redirect_to :action => :index redirect_to :action => :index
else else
render :action => :new render :action => :new
@ -43,6 +44,7 @@ class Panel::UsersController < ApplicationController
@user.avatar = params[:file] if params[:file] @user.avatar = params[:file] if params[:file]
if @user.update_attributes(params[:user]) if @user.update_attributes(params[:user])
flash[:notice] = t('panel.update_success_user')
redirect_to :action => :index redirect_to :action => :index
else else
@user_info_models = UserInfoModel.all.entries @user_info_models = UserInfoModel.all.entries
@ -52,8 +54,12 @@ class Panel::UsersController < ApplicationController
end end
def destroy def destroy
@user = User.find(params[:id]) if params[:id].eql?(session['warden.user.user.key'][1].to_s)
@user.destroy flash[:error] = t('panel.cant_delete_self')
else
@user = User.find(params[:id])
@user.destroy
end
redirect_to :action => :index redirect_to :action => :index
end end

View File

@ -1,11 +1,31 @@
module ApplicationHelper module ApplicationHelper
FLASH_NOTICE_KEYS = [:error, :notice, :warning]
def colorize_in_use_locale(locale) def colorize_in_use_locale(locale)
@site_in_use_locales.include?(locale)? 'green' : 'red' @site_in_use_locales.include?(locale)? 'green' : 'red'
end end
def flash_messages
return unless messages = flash.keys.select{|k| FLASH_NOTICE_KEYS.include?(k)}
formatted_messages = messages.map do |type|
content_tag :div, :class => type.to_s do
message_for_item(flash[type], flash["#{type}_item".to_sym])
end
end
raw(formatted_messages.join)
end
def link_back def link_back
link_to t('back'), session[:last_page] link_to t('back'), session[:last_page]
end end
def message_for_item(message, item = nil)
if item.is_a?(Array)
message % link_to(*item)
else
message % item
end
end
end end

View File

@ -12,6 +12,8 @@
</ul> </ul>
<% end -%> <% end -%>
<%= flash_messages %>
<h1><%= t('admin.list_items') %>: <%= show_parent_items_link unless @parent_item.nil? %></h1> <h1><%= t('admin.list_items') %>: <%= show_parent_items_link unless @parent_item.nil? %></h1>
<% if !Layout.exist_one? %> <% if !Layout.exist_one? %>

View File

@ -4,6 +4,8 @@
</ul> </ul>
<% end -%> <% end -%>
<%= flash_messages %>
<h1><%= t('admin.list_layouts') %></h1> <h1><%= t('admin.list_layouts') %></h1>
<table> <table>

View File

@ -32,7 +32,7 @@
</fieldset> </fieldset>
<% end %> <% end %>
<%= hidden_field_tag :active_roles, @user.active_roles.size > 0 %> <%= hidden_field_tag :active_roles, (@user.active_roles.size > 0 rescue nil ) %>
<% if @user_role_models %> <% if @user_role_models %>
<fieldset id="roles" <%= raw(' style="display: none;"') unless (@user.active_roles.size > 0 rescue nil) %>> <fieldset id="roles" <%= raw(' style="display: none;"') unless (@user.active_roles.size > 0 rescue nil) %>>
<legend><%= t('panel.roles') %></legend> <legend><%= t('panel.roles') %></legend>

View File

@ -1,9 +1,11 @@
<% content_for :secondary do %> <% content_for :secondary do %>
<ul class="list"> <ul class="list">
<li><%= link_to t('panel.new_user'), new_panel_user_path, :class => 'button positive' %></li> <li><%= link_to t('panel.new_user'), new_panel_user_path, :class => 'button positive' %></li>
</ul> </ul>
<% end -%> <% end -%>
<%= flash_messages %>
<h1><%= t('panel.list_users') %></h1> <h1><%= t('panel.list_users') %></h1>
<table> <table>
@ -14,15 +16,16 @@
<th><%= t('panel.action') %></th> <th><%= t('panel.action') %></th>
</tr> </tr>
<% @users.each do |user| %> <% @users.each do |user| %>
<tr> <tr>
<td><%= user.get_roles %></td> <td><%= user.get_roles %></td>
<td><%= user[:name] %></td> <td><%= user[:name] %></td>
<td><%= user.email %></td> <td><%= user.email %></td>
<td> <td>
<%= link_to t(:show), panel_user_path(user) %> | <%= link_to t(:show), panel_user_path(user) %> |
<%= link_to t(:edit), edit_panel_user_path(user) %> | <%= link_to t(:edit), edit_panel_user_path(user) %> |
<%= link_to t(:delete), panel_user_path(user), :method => :delete %></td> <%= link_to t(:delete), panel_user_path(user), :confirm => t('sure?'), :method => :delete %>
</tr> </td>
<% end %> </tr>
<% end %>
</table> </table>

View File

@ -97,6 +97,8 @@ en:
panel: panel:
action: Action action: Action
cant_delete_self: You can not delete yourself.
create_success_user: User was successfully created.
email: Email email: Email
home: Home home: Home
list_users: Users list list_users: Users list
@ -105,5 +107,6 @@ en:
new_user: New user new_user: New user
infos: Information infos: Information
roles: Roles roles: Roles
update_success_user: User was successfully updated.
user: User user: User
user_panel: User panel user_panel: User panel

View File

@ -94,6 +94,8 @@ zh_tw:
panel: panel:
action: 行動 action: 行動
cant_delete_self: 您不可以刪除自己。
create_success_user: 用戶已成功創建。
email: Email email: Email
home: 首頁 home: 首頁
list_users: 使用清單 list_users: 使用清單
@ -102,6 +104,7 @@ zh_tw:
new_user: 新增使用 new_user: 新增使用
infos: 資料 infos: 資料
roles: 角色 roles: 角色
update_success_user: 用戶已成功更新。
user: 用戶 user: 用戶
user_panel: 用戶面板 user_panel: 用戶面板

View File

@ -10,3 +10,15 @@ ul.list li{
list-style:none; list-style:none;
border-bottom:1px solid #ccc; border-bottom:1px solid #ccc;
} }
.error{
color:red;
}
.notice, .message{
color:green;
}
.warning{
color:orange;
}