in this chapter, we will add destroy action to users controller to finish REST.
first, we need to add administrative users who can use the destroy action. so we need to add a new attr of admin which is a boolean.
and in the model, you can use admin? to get this attr value.
1. let's write the unit test first.:
describe "admin attribute" do before(:each) do @user = User.create!(@attr) end it "should respond to admin" do @user.should respond_to(:admin) end it "should not be an admin by default" do @user.should_not be_admin end it "should be convertible to an admin" do @user.toggle!(:admin) @user.should be_admin end end end
note, we use
@user.toggle!(:admin)
rails can detect the boolean attr, and auto add a new question mark method of admin?
2. next, let's add a new attr to users table.
rails g migration add_admin_to_users admin:boolean
this wiil add a new column, and the default value is nil.
so to be clear, you can change the migration file:
add_column :users, :admin, :boolean, :default => false
3. next, we need to migrate to database, and also do test:prepare to update test database.
rake db:migrate rake db:test:prepare
4. revisiting attr_accessible
you may wondering, why we use toggle!, instead of assign ":admin=>true" directly?
because this will not work, as we defined attr_accessible, and :admin is not in the list.
so you can't do mass assignment for admin column.
explicily define attr_accessible is crucial for a good site security. if you omit it, any user can issue such request:
put /users/17?admin=1
then this user can do everything bad.
so define attr_accessible for every model is a good practice.
5. adding destroy action:
first, let's add delete link to the user's list page:
<li> <%= gravatar_for user, :size => 30 %> <%= link_to user.name, user %> <% if current_user.admin? %> | <%= link_to "delete", user, :method => :delete, :confirm => "You sure?", :title => "Delete #{user.name}" %> <% end %> </li>
note:
the method is ":delete", so this link will issue a DELETE request.
Web browsers can't send DELETE link natively, so rails fakes with javascript.
But to get this delete link to work, we have to include rails default javascript library, :defaults.
<%= javascript_include_tag :defaults %>
this line code is in app/views/layouts/application.html.erb
def destroy User.find_by_id(params[:id]).destroy flash[:success] = "User destroied." redirect_to users_path end
note:
User.find_by_id(params[:id]).destroy
发表评论
-
12.3.3 scaling issue of the status feed
2011-10-30 17:54 814the problem of the implementati ... -
12.3 the status feed
2011-10-30 15:34 8521. we need to get all the micro ... -
12.2 a working follow button with Ajax
2011-10-29 18:10 9091. in the last chapter, in the ... -
12.2 a web interface for following and followers.
2011-10-28 22:14 8751.before we do the UI, we need ... -
12. following user, 12.1 relationship model
2011-10-18 14:29 7451. we need to use a relationshi ... -
11.3 manipulating microposts.
2011-10-17 15:31 8931. since all micropost actions ... -
11.2 show microposts.
2011-10-17 12:01 6961. add test to test the new use ... -
11.1 user micropost -- a micropost model.
2011-10-17 10:43 11031. we will first generate a mic ... -
10.3 showing users list
2011-10-15 20:41 768in this chapter, we will do use ... -
10.2 protect pages.
2011-10-15 15:11 655again, we will start from TD ... -
10.1 updating users.
2011-10-14 18:30 7041. git checkout -b updating-use ... -
9.4 sign out
2011-10-13 15:21 731whew!!!, last chapter is a long ... -
9.3 sign in success.
2011-10-12 15:39 7411. we will first finish the cre ... -
9.1 about flash.now[:error] vs flash[:error]
2011-10-12 15:37 722There’s a subtle difference ... -
9.2 sign in failure
2011-10-12 12:19 655start from TDD!!! 1. requir ... -
9.1 sessions
2011-10-12 10:00 643a session is a semi-permanent c ... -
what test framework should you use?
2011-10-11 16:56 0for integration test, i have no ... -
what test framework should you use?
2011-10-11 16:56 0<p>for integration test, ... -
8.4 rspec integration tests
2011-10-11 16:53 714in integration test, you can te ... -
8.3 sign up success
2011-10-11 14:39 775Chapter 8.3 this part, we will ...
相关推荐
- Destroying..4 - Destroying..3 - Destroying..2 - Destroying..1 - Destroying..0 程序首先创建了一个Samp类型的动态数组,然后逐个设置元素值并输出,最后释放内存。析构函数在每个对象被删除时执行,显示...
Destroying..2 Destroying..1 Destroying..0 该资源摘要信息提供了一个完整的C++语言程序设计期末考试试题,涵盖了面向对象编程、函数重载、递归调用、拷贝构造函数、继承、多态性等C++编程概念,为C++语言学习者和...
OPC客户端,测试连接opc服务端,绝对好用,自动识别服务端,自动识别组和项
《TongWeb 7.0 应用开发手册》是为开发者提供的一份详尽指南,专注于在TongWeb平台上进行高效、稳定的应用程序开发。TongWeb是东方通科技推出的一款高性能的企业级Java应用服务器,它支持最新的Servlet规范和其他Web...
这个Heroku CLI插件可以清理您的旧应用程序。 在此插件的上下文中,“临时应用”是您所拥有的名为foo-bar-1234的名称,没有其他合作者...destroying apps... done 安装 $ heroku plugins:install heroku-destroy-temp
Contents Introduction Course Materials..................Creating and Destroying Objects...........................................................................16 Demonstration: Creating Classes.....
Creating and Destroying Objects.......................................................................16 Demonstration: Creating Classes..................................................................
Creating and Destroying Objects.................................................................16 Demonstration: Creating Classes.................................................................23 ...
Creating and Destroying Objects.................................................................16 Demonstration: Creating Classes.................................................................23 ...
System.out.println("Destroying..."); } } ``` ##### 4. `@Qualifier` 用于指定需要注入的具体 Bean。 - **示例**: ```java @Component public class SampleService { private final ...
7. **创建与销毁对象(Creating and Destroying Objects)**:"CSharp_Module 9_Creating and Destroying Objects.pdf"将涵盖对象的生命周期,包括构造函数、析构函数和垃圾回收机制,以及何时和如何适当地分配和释放...
The changelog below should be helpful to you in getting Antnet into ns2.34 without destroying your additions. Instructions copy the files provided into NS2.34 directory as is. Run: ./configure make...
To prevent accidently destroying data. - Simultaneous image creation is now supported. See the help documentation for naming convention used. - Running imageUSB with -l command line will save a log ...
This way you can edit the underlying topology without losing or destroying your chamfers. Also chamfering by edge weights allows to have variable widths on your chamfers. Here are some of the ...
模块9:《C# - Module 9_Creating and Destroying Objects.pdf》 本模块讲解了对象的生命周期,包括构造函数和析构函数的使用,以及垃圾回收机制。学习者将了解如何在适当的时间创建和释放对象,以优化内存管理。 ...
RxLifecyle is a library that can help you to unsubscribe the observable sequences automatically when a activity or fragment is destroying. There are some differences between this library and trello/...
print "Destroying a RomanNumeralConverterTest...." self.cvt = None ``` `unittest.TestCase`类中的`test_parsing_millenia`方法是一个测试方法,它通过`self.assertEquals`断言函数来验证`...
在IT领域,尤其是在编程语言的学习与应用中,C#(C Sharp)作为一款由微软开发的面向对象的、类型安全的、垃圾回收的程序设计语言,其在创建和销毁对象方面的处理方式是开发者必须掌握的核心技能之一。...
Several researchers have recently pro-posed schemes to parallelize SGD, but all require performance-destroying memory locking andsynchronization. This work aims to show using novel theoretical ...