`
peryt
  • 浏览: 55270 次
  • 来自: ...
最近访客 更多访客>>
社区版块
存档分类
最新评论
  • waiting: 既然都指定了dataType为'script'那就不必特别在b ...
    jQuery

10.4 destroying users.

 
阅读更多

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
this is how to destroy a record from the database.
分享到:
评论

相关推荐

    C++语言程序设计第三版试卷

    - Destroying..4 - Destroying..3 - Destroying..2 - Destroying..1 - Destroying..0 程序首先创建了一个Samp类型的动态数组,然后逐个设置元素值并输出,最后释放内存。析构函数在每个对象被删除时执行,显示...

    C++语言程序设计__期末考试试题及答案.pdf

    Destroying..2 Destroying..1 Destroying..0 该资源摘要信息提供了一个完整的C++语言程序设计期末考试试题,涵盖了面向对象编程、函数重载、递归调用、拷贝构造函数、继承、多态性等C++编程概念,为C++语言学习者和...

    OPC客户端,测试连接opc服务端

    OPC客户端,测试连接opc服务端,绝对好用,自动识别服务端,自动识别组和项

    TongWeb7应用开发手册.pdf

    《TongWeb 7.0 应用开发手册》是为开发者提供的一份详尽指南,专注于在TongWeb平台上进行高效、稳定的应用程序开发。TongWeb是东方通科技推出的一款高性能的企业级Java应用服务器,它支持最新的Servlet规范和其他Web...

    heroku-destroy-temp:Heroku CLI插件可销毁临时应用

    这个Heroku CLI插件可以清理您的旧应用程序。 在此插件的上下文中,“临时应用”是您所拥有的名为foo-bar-1234的名称,没有其他合作者...destroying apps... done 安装 $ heroku plugins:install heroku-destroy-temp

    Programming with MS VB.NET.pdf

    Contents Introduction Course Materials..................Creating and Destroying Objects...........................................................................16 Demonstration: Creating Classes.....

    VB.Net Programming.pdf

    Creating and Destroying Objects.......................................................................16 Demonstration: Creating Classes..................................................................

    MSDN Traning - VB.NET (VBL).pdf

    Creating and Destroying Objects.................................................................16 Demonstration: Creating Classes.................................................................23 ...

    Programming with Microsoft Visual Basic.NET_Delivery Guide.pdf

    Creating and Destroying Objects.................................................................16 Demonstration: Creating Classes.................................................................23 ...

    40 个 SpringBoot 常用注解:让生产力爆表!

    System.out.println("Destroying..."); } } ``` ##### 4. `@Qualifier` 用于指定需要注入的具体 Bean。 - **示例**: ```java @Component public class SampleService { private final ...

    微软.NET程序员高级培训教程系列资料.C#(下)【英文版】

    7. **创建与销毁对象(Creating and Destroying Objects)**:"CSharp_Module 9_Creating and Destroying Objects.pdf"将涵盖对象的生命周期,包括构造函数、析构函数和垃圾回收机制,以及何时和如何适当地分配和释放...

    ns2.34的蚁群算法补丁

    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...

    U盘量产工具

    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 ...

    Quad Chamfer Modifier 1.16

    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 ...

    Msdn Training - Microsoft Visual Studio Net (With Source)

    模块9:《C# - Module 9_Creating and Destroying Objects.pdf》 本模块讲解了对象的生命周期,包括构造函数和析构函数的使用,以及垃圾回收机制。学习者将了解如何在适当的时间创建和释放对象,以优化内存管理。 ...

    Android代码-RxLifecycle

    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/...

    python自动化测试之setUp与tearDown实例.pdf

    print "Destroying a RomanNumeralConverterTest...." self.cvt = None ``` `unittest.TestCase`类中的`test_parsing_millenia`方法是一个测试方法,它通过`self.assertEquals`断言函数来验证`...

    CSharp - Module 9_Creating and Destroying Objects

    在IT领域,尤其是在编程语言的学习与应用中,C#(C Sharp)作为一款由微软开发的面向对象的、类型安全的、垃圾回收的程序设计语言,其在创建和销毁对象方面的处理方式是开发者必须掌握的核心技能之一。...

    HOGWILD演算法

    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 ...

Global site tag (gtag.js) - Google Analytics