私はRORの初心者で、オンラインショップを建設しています。追加する製品の数を増やすためにドロップダウンリストを作成しようとしています同時にカートに入れてください。 コントローラーにリンクされたフォームを作成しましたが、なぜそれが全く機能しないのか分かりません。フォームを表示しますが、常に要素をカートに1つずつ追加します。私は私のリストに数量を選択することができます、しかし、それが認識されないと、私はここでボタンレール、カート内のアイテム数を増やすためのドロップダウンリスト
を「追加」をクリックすると、一つの要素を追加し、私の店のビューは次のとおりです。
<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
希望私を助けるのに役立ちます!おかげさまで
"なぜ私はそれがまったく機能しないのかわかりません"と言うとき、あなたは現在の行動が何であるかを詳述できますか? –
はい、確かです。常に要素を1つずつ追加します。リスト内の数量を選択できますが、「追加」ボタンをクリックすると認識されずに1つの要素が追加されます –
追加された広告申込情報は保存されていますが、送信ボタンはありませんあなたのフォーム、それは決してそれ自体を保存しません。この種のものについては、jQueryを使う方がよいでしょう。 (あなたとあなたの後にコードを読んでいる人にとっては、あなたにとってより簡単になります) – luissimo