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

  • 相关阅读:
    练手
    课余时间娱乐下
    2017-2-19,作业
    JavaScript(下)
    JavaScript(上)
    Day15:网络编程-HTTP
    小知识:静态导入
    多线程知识点:锁
    多线程知识点:同步
    Day16:反射技术
  • 原文地址:https://www.cnblogs.com/hwpayg/p/2471677.html
Copyright © 2011-2022 走看看