Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 用轨道建造购物车_Ruby On Rails 3_Shopping Cart - Fatal编程技术网

Ruby on rails 3 用轨道建造购物车

Ruby on rails 3 用轨道建造购物车,ruby-on-rails-3,shopping-cart,Ruby On Rails 3,Shopping Cart,rails的新特性。我在网上看过一些教程,但我无法获得简单的购物车功能。下面是我的代码和我得到的错误 class CartsController < ApplicationController def add @current_cart = @cart @current_cart = @current_cart.products.new(product_params) @current_cart.save session[:current_cart_id]

rails的新特性。我在网上看过一些教程,但我无法获得简单的购物车功能。下面是我的代码和我得到的错误

class CartsController < ApplicationController

def add
    @current_cart = @cart
    @current_cart = @current_cart.products.new(product_params)
    @current_cart.save
    session[:current_cart_id] = @current_cart.id

    redirect_to :back
  end



def delete
  end

  def show
    #@cart = initialize_cart.cart
  end

  private

  def product_params
    params.permit(:id)
  end

end
错误:

ActiveModel::CartsController中的未知属性错误#添加

产品的未知属性“购物车id”

提取的源(第5行附近):


任何帮助或指导都将不胜感激。谢谢

您可以添加您的
产品
购物车
型号吗?您的
产品
表是否有
购物车id
列?
参数require(:Product).permit(:id)
,然后您需要向请求发送产品[:id]
class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  before_filter :initialize_cart

  def initialize_cart
    if session[:cart]
      @cart = Cart.find(session[:cart])
    else
      @cart = Cart.create
      session[:cart] = @cart.id
    end
  end

end
<%= link_to "Add to Cart", :controller => "carts", :action => "add", :id => @product.id, :class => "btn bg-turquoise"%>
get 'carts/add/:id', to: 'carts#add'

get 'carts/delete'

get 'carts/show', to: 'carts#show', as: 'showcart'
3 def add
4   @current_cart = @cart
**5     @current_cart = @current_cart.products.new(product_params)**
6   @current_cart.save
7   session[:current_cart_id] = @current_cart.id