zoukankan      html  css  js  c++  java
  • Agile Web Development 4th ed. Can't mass assign.error

    I am new to Ruby and to Rails (using Ruby 1.9.3 and Rails 3.2.3) so I
    picked up the Agile Web Development book and have been working through
    it (apologies if this isn't the correct place for this question but the
    pragprog forums don't seem to work for me).  I am currently working
    through the book and have reached the point where I am adding
    functionality to an "Add to Cart" button.  Here is the code that was
    provided for the 'create' method in the line_item_controller: 

    def create

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

    solution:

    1.@line_item = @cart.line_items.build
      @line_item.product = product 

    2.

    class LineItem < ActiveRecord::Base
    belongs_to :product
    belongs_to :cart
    attr_accessible :cart_id, :product_id
    attr_accessible :product

    =begin

    If you want to be able to do line_items.build(product: p) you need to
    mark product as attr_accessible too. The mass assignment stuff doesn't
    know what things are database attributes, virtual attributes,
    associations etc. 

    =end

    end

  • 相关阅读:
    数据库分表分库
    rabbitMq 集群
    马哥博客作业第七周
    马哥博客作业第六周
    马哥博客作业第一阶段考试
    马哥博客作业第四周
    马哥博客作业第三周
    马哥博客作业第二周
    马哥博客作业第一周
    03-MySQL数据库表的基本操作
  • 原文地址:https://www.cnblogs.com/hwpayg/p/2471677.html
Copyright © 2011-2022 走看看