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

  • 相关阅读:
    第04组 团队项目-需求分析报告
    团队项目-选题报告
    第二次结对编程作业
    第一次结对编程作业
    第四组 团队展示
    第一次博客作业
    第09组 Alpha事后诸葛亮
    第09组 Alpha冲刺(6/6)
    2019 SDN上机第4次作业
    第09组 Alpha冲刺(5/6)
  • 原文地址:https://www.cnblogs.com/hwpayg/p/2471677.html
Copyright © 2011-2022 走看看