2012-05-23 20 views
1

我正在關注敏捷Web開發第四版的Rails。在第9章112頁。我試圖運行耙測試:功能。我想我跟着他們的每一段代碼,但它給我一個質量分配錯誤。當我運行的服務器,它給我這個錯誤rake test:函數不工作 - 錯誤MassAssignmentSecurity:錯誤

::加載ActiveModel :: MassAssignmentSecurity中的錯誤LineItemsController#創建

無法大規模指派保護的屬性:產品

這是LineItemsController如何創建函數看起來像

def create 
    @cart = current_cart 
    product = Product.find(params[:product_id]) 
    @line_item = @cart.line_items.build(product: product) 

    respond_to do |format| 
     if @line_item.save 
     format.html { redirect_to @line_item.cart, 
      notice: 'Line item was successfully created.' } 
     format.json { render json: @line_item, 
      status: :created, location: @line_item } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @line_item.errors, 
      status: :unprocessable_entity } 
     end 
    end 
    end 

以下是在line_items_controller_test.rb在文件夾測試/功能的創建測試/

test "should create line_item" do 
    assert_difference('LineItem.count') do 
     post :create, product_id: products(:ruby).id 
    end 

    assert_redirected_to cart_path(assigns(:line_item).cart) 
    end 

我錯過了什麼?

回答

4

這是Rails的新版本的最近變化,由於GitHub的慘敗:https://github.com/rails/rails/commit/b83965785db1eec019edf1fc272b1aa393e6dc57

爲了解決這個問題,你可以做兩件事情之一:

1)更改此設置(在你的配置/application.rb文件)false,使大衆分配網站廣泛

config.active_record.whitelist_attributes = false 

2)所有的白名單,你會在你的模型某處添加此行來改變屬性:

attr_accessible :product 

第一種是舊的默認方式,而且更容易。其次是生產應用程序更安全。

+1

感謝這幫助 – user1372829

相關問題