RoR教程Michael Hartl删除Microposts也删除了用户

时间:2013-07-23 15:50:07

标签: jquery ruby-on-rails

我在尝试在Hartl的Ruby on Rails教程中实现示例应用程序时遇到了一个奇怪的问题。

当我删除微博时,会发生以下情况:

  1. 微博被删除。
  2. 用户也被删除。
  3. 转到“注册”页面。
  4. 只有第一步应该发生。用户不应该被删除。删除微博时,控制台跟踪显示以下内容:

    Started DELETE "/microposts/1" for 127.0.0.1 at 2013-07-23 11:35:31 -0400
    Processing by MicropostsController#destroy as HTML
      Parameters: {"authenticity_token"=>"clfdQ1F/1ewiDnuae9OpVXSZ3S/wtieCrVYNM+Y1838=", "id"=>"1"}
      User Load (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'cUs3NX6FrzU-XnkCbGE4wg' LIMIT 1
      Micropost Load (1.0ms)  SELECT "microposts".* FROM "microposts" WHERE "microposts"."user_id" = 1 AND "microposts"."id" = 1 ORDER BY microposts.created_at DESC LIMIT 1
       (0.0ms)  begin transaction
      SQL (2.0ms)  DELETE FROM "microposts" WHERE "microposts"."id" = ?  [["id", 1]]
      User Load (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
      SQL (1.0ms)  DELETE FROM "users" WHERE "users"."id" = ?  [["id", 1]]
       (12.0ms)  commit transaction
    Redirected to http://localhost:3000/
    Completed 302 Found in 27ms (ActiveRecord: 16.0ms)
    

    我不确定为什么要发送DELETE FROM“用户”。

    我的application.js文件如下所示:

    //= require jquery
    //= require jquery_ujs
    //= require bootstrap
    //= require_tree .
    

    我的routes.rb看起来像这样:

    SampleApp::Application.routes.draw do
    
      resources :users
      resources :sessions,    only: [:new, :create, :destroy]
      resources :microposts,  only: [:create, :destroy]
    
      root :to => 'static_pages#home'
      match '/signup',  to: 'users#new'
      match '/signin',  to: 'sessions#new'
      match '/signout', to: 'sessions#destroy', via: :delete
      match '/help',    to: 'static_pages#help'
      match '/about',   to: 'static_pages#about'
      match '/contact', to: 'static_pages#contact'
    end
    

    microposts_controller.rb看起来像这样:

    class MicropostsController < ApplicationController
    
      before_filter :signed_in_user, only: [:create, :destroy]
      before_filter :correct_user,   only: :destroy
    
      def create
        @micropost = current_user.microposts.build(params[:micropost])
        if @micropost.save
            flash[:success] = "Micropost created!"
            redirect_to root_path
        else
          @feed_items = []
            render 'static_pages/home'
        end
      end
    
      def destroy
        @micropost.destroy
        redirect_back_or root_path
      end
    
      private
    
       def correct_user
         @micropost = current_user.microposts.find_by_id(params[:id])
         redirect_to root_path if @micropost.nil?
       end
    end
    

    您可以在上面看到如何实现destroy动作(如教程中所述)。所以我不确定问题是什么。

    这是app / models / micropost.rb

    class Micropost < ActiveRecord::Base
      # here we removed :user_id from attr_accessable for security reasons
      attr_accessible :content
      belongs_to :user, dependent: :destroy
    
      validates :content, presence: true, length: { maximum: 140 } 
      validates :user_id, presence: true
    
      default_scope order: 'microposts.created_at DESC'
    end
    

    感谢您的任何帮助。

2 个答案:

答案 0 :(得分:2)

问题出在Micropost模型中。以下行是删除与user相关联的Micropost

belongs_to :user, dependent: :destroy

将该行替换为:

belongs_to :user

并且您不会看到用户关联被删除。

请在此处阅读有关“从属关联”的文档:http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

<强>更新

如果您要删除属于用户的所有微博,则应在dependent: :destroy中添加User.rb,如下所示:

has_many :microposts, dependent: :destroy 

答案 1 :(得分:0)

您的Micropost课程表示,当使用dependent: :destroy上的belongs_to :user选项销毁微博时,应销毁该用户。

:dependent dependent: :destroy {{1}}如果设置为:destroy,当此对象被销毁时,相关对象将被销毁。

因此,如果您删除{{1}},则无法解决问题。