Ruby on rails 未定义的局部变量或方法“乘积';对于#<#&书信电报;类别:0x007fe77c4f3c68>;:0x007fe77c69cb78>;
我正在构建的应用程序有问题 在Ruby on rails 未定义的局部变量或方法“乘积';对于#<#&书信电报;类别:0x007fe77c4f3c68>;:0x007fe77c69cb78>;,ruby-on-rails,ruby,ruby-on-rails-4,undefined,Ruby On Rails,Ruby,Ruby On Rails 4,Undefined,我正在构建的应用程序有问题 在products/show.html.erb中,我有此代码将产品添加到购物车 <%= button_to product_items_path(product_id: product) do %> <i class="fa fa-shopping-cart"></i>Add to Cart <% end %> 只有实例变量可用于视图 def create @product = Prod
products/show.html.erb
中,我有此代码将产品添加到购物车
<%= button_to product_items_path(product_id: product) do %>
<i class="fa fa-shopping-cart"></i>Add to Cart
<% end %>
只有实例变量可用于视图
def create
@product = Product.find(params[:product_id]) # Prefix variable name with @
@product_item = @cart.add_product(product.id)
if @product_item.save
redirect_to root_url, notice:'Product added to Cart'
else
render :new
end
end
你的看法是:
<%= button_to product_items_path(@product) do %>
<i class="fa fa-shopping-cart"></i>Add to Cart
<% end %>
添加到购物车
您应该能够将对象传递给_pathhelper。只有实例变量可用于视图
def create
@product = Product.find(params[:product_id]) # Prefix variable name with @
@product_item = @cart.add_product(product.id)
if @product_item.save
redirect_to root_url, notice:'Product added to Cart'
else
render :new
end
end
你的看法是:
<%= button_to product_items_path(@product) do %>
<i class="fa fa-shopping-cart"></i>Add to Cart
<% end %>
添加到购物车
您应该能够将对象传递给_pathhelper。它需要是一个实例变量,才能从视图中访问。(
@product
而不是这两个位置的product
)它需要是一个实例变量才能从视图中访问。(@product
而不是product
在两个位置)
<%= button_to product_items_path(@product) do %>
<i class="fa fa-shopping-cart"></i>Add to Cart
<% end %>