In this second part of the series on administration, you will learn how to lock down the site to keep the public from accessing the administration features.
在介绍管理部分系列的第二部分,你将学习如何限制访问权限的相关内容。
上一节加了三个admin链接,现在对外是都可见的。
现在需要先在link上加上权限
<% if admin?%>
<div class="actions">
<%= link_to "Edit", edit_episode_path(episode) %>
<%= link_to "Destroy", episode_path(episode), :confirm => "Are you sure?", :method => :delete %>
</div>
<%= link_to "Edit", edit_episode_path(episode) %>
<%= link_to "Destroy", episode_path(episode), :confirm => "Are you sure?", :method => :delete %>
</div>
<% end %>
...
<% if admin?%>
<div class="actions content">
<%= link_to "New Episode", new_episode_path %>
</div>
<%= link_to "New Episode", new_episode_path %>
</div>
<%end%>
但是现在还没有admin?方法,这个方法需要在helper中定义。
但是我们还希望在controller中也调用这个方法,所以写在
application.rb中
helper_method :admin?
protected
def admin?
false
end
好了,这样就隐藏了。
但是通过输入url还可以访问对应的操作。
所以还需要在controller中加入before_filter :authorize, :except=>:index
再在application.rb中加入
protected
def authorize
unless admin?
flash[:error]="unauthorized access"
redirect_to home_path
false
end
end
def admin?
false
end
本文转自 fsjoy1983 51CTO博客,原文链接:http://blog.51cto.com/fsjoy/131756,如需转载请自行联系原作者