2016-09-13 15 views
0

私は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
私が作成したメソッドでは、数量を定義しました:params.require(:line_item).permit(:quantity)で許可しました。

希望私を助けるのに役立ちます!おかげさまで

+0

"なぜ私はそれがまったく機能しないのかわかりません"と言うとき、あなたは現在の行動が何であるかを詳述できますか? –

+0

はい、確かです。常に要素を1つずつ追加します。リスト内の数量を選択できますが、「追加」ボタンをクリックすると認識されずに1つの要素が追加されます –

+0

追加された広告申込情報は保存されていますが、送信ボタンはありませんあなたのフォーム、それは決してそれ自体を保存しません。この種のものについては、jQueryを使う方がよいでしょう。 (あなたとあなたの後にコードを読んでいる人にとっては、あなたにとってより簡単になります) – luissimo

答えて

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を変更することもできます。

これは単なるアイデアを提供するためのものです。私はあなたのコードを知らないので、この問題を回避する必要があります。

+0

これは私のために働いたことはありません..存在しないいくつかのルートのこの検索。 –

+0

これは正確な答えではないので、あなたの投稿に必要なコードがすべて表示されないので、正確な答え。しかし、これはあなたにとってうまくいくはずです。あなたは<%= form_for(item)do | f |を回避するだけです。 %>など。私はそれを作ったので – luissimo

+0

私のコードのどの部分を私にもっと正確な答えを与える必要がありますか?私はRoRの初心者であり、これは私をたくさんブロックする部分です。あなたがもっとそれを手助けできるなら、それは素晴らしいでしょう:) –

関連する問題