我是ROR的初学者,我正在建立一个在线商店,我正在尝试做一个下拉列表,以增加同时添加到购物车中的产品数量。 我做了一个表单,与控制器链接,但我不知道为什么它根本不起作用,它显示了表单,但它总是逐个添加元素到购物车。我可以在列表中选择一个数量但是当我点击“添加”按钮时它无法识别并添加一个元素
这是我的店铺观点:
<div class="popup-price-and-add">
<span class="popup-price"><%=number_to_currency(product.price)%></span><br/>
<%= text_field_tag 'quantity' %>
<%= form_for(line_items_path(:set_quantity)) do |form|%>
<p>
<%= form.label :quantity%>
<%= form.select :quantity, (1..10) %>
<p>
<% end %>
<%= link_to image_tag("/elem/Btn-add.png", :class =>"popup-add"), line_items_path(product_id: product), remote: true, :method => :post, :class => "popup-add" %>
</div>
这是我的line_items_controller:
class LineItemsController < ApplicationController
skip_before_action :authorize, only: [:create, :destroy]
include CurrentCart
before_action :set_cart, only: [:create]
before_action :set_line_item, only: [:show, :edit, :update, :destroy]
# GET /line_items
# GET /line_items.json
def index
@line_items = LineItem.all
end
# GET /line_items/1
# GET /line_items/1.json
def show
end
# GET /line_items/new
def new
@line_item = LineItem.new
end
# GET /line_items/1/edit
def edit
end
# POST /line_items
# POST /line_items.json
def create
session[:counter] = 0
quantity = params[:quantity]
product = Product.find(params[:product_id])
@line_item = @cart.add_product(product.id)
respond_to do |format|
if @line_item.save
format.html { redirect_to @line_item.cart, notice: 'Line item was successfully created.' }
format.json { render action: 'show', status: :created, location: @line_item }
else
format.html { render action: 'new' }
format.json { render json: @line_item.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /line_items/1
# PATCH/PUT /line_items/1.json
def update
respond_to do |format|
if @line_item.update(line_item_params)
format.html { redirect_to @line_item, notice: 'Line item was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @line_item.errors, status: :unprocessable_entity }
end
end
end
# DELETE /line_items/1
# DELETE /line_items/1.json
def destroy
@line_item = LineItem.find(params[:id])
@line_item.destroy
respond_to do |format|
if current_cart.line_items.empty?
format.html{redirect_to(store_url, :notice=>'Your cart is empty')}
else
format.html { redirect_to(@line_item.cart, :notice=> 'Item has been removed from your cart.') }
end
format.xml { head :ok}
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_line_item
@line_item = LineItem.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def line_item_params
params.require(:line_item).permit(:product_id)
params.require(:line_item).permit(:quantity)
end
end
如你所见,在我的创建方法中,我定义了一个数量,我允许它:params.require(:line_item).permit(:quantity)。
希望它会帮助你帮助我! 感谢。
答案 0 :(得分:0)
您应该为具有数量列的项目创建数据库表,但如果您已经拥有项目模型但没有数量值,则可以执行此操作
rails g migration AddQuantityToItems quantity:integer
Rake db:migrate
然后你需要在视图中做这样的事情:
<%= form_for(Item) do |f| %>
<%= f.hidden_field :quantity, class: 'cart_quantity' %>
<p class="cart_quantity"></p>
<button class='quantity_down'></button>
<button class='quantity_up'></button>
<%= f.submit 'Add to cart' %>
<% end %>
的jQuery
var quantity = 0;
$(document).on('click', 'quantity_down', function() {
quantity -= 1
$('cart_quantity').text(quantity);
$('cart_quantity').val(quantity);
});
$(document).on('click', 'quantity_up', function() {
quantity += 1;
$('cart_quantity').text(quantity);
$('cart_quantity').val(quantity);
});
如果您确实需要一个下拉列表,那么也可以通过选择下拉列表切换按钮并相应地更改jQuery。
这只是为了给你一个想法,我不知道你的代码所以你必须解决这个问题。