Ich bin Anfänger in HTML und Ruby auf Schienen. Es klingt eine sehr grundlegende Frage, aber nach vielen Versuchen bekomme ich es nicht, wie man Wert von einem Eingangstyp übergibt, der nicht in einer Form zu einem Controller ist.Wie übergeben Wert von Eingabe-Typ in HTML zu einem Controller in Schienen
Dies ist mein Code -
<div style = "border:1px solid black; padding:1em;">
<%= link_to product.name, product_path(product.id) %>
<br><br>
Price : <%= product.price %>
Quantity : <input type="text" name='quantity' value = '1' min ='1'/>
<br><br>
<% if session[:cart_id] && @cart_item_ids.include?(product.id) %>
<button><%= link_to 'Remove', remove_item_path(:product_id => product.id)
%></button>
<% elsif session[:cart_id] %>
<button><%= link_to 'Add To Cart', add_to_cart_path(:product_id => product.id, :price => product.price, :quantity => 'quantity')
%></button>
<% end%>
<br>
</div>
Das ist mein Controller ist
class CartsController < ApplicationController
def add_to_cart
byebug
cart_item = CartItem.new
cart_item.product_id = params[:product_id]
cart_item.quantity = 1
cart_item.price = params[:price]
cart_item.cart_id = session[:cart_id]
cart_item.save
product = Product.find(params[:product_id])
category = Category.find(product.category_id)
redirect_to category_path(:id => product.category_id)
end
def remove_item
CartItem.where(:cart_id => session[:cart_id]).where(:product_id => params[:product_id]).first.destroy
product = Product.find(params[:product_id])
redirect_to category_path(:id => product.category_id)
end
def view_cart
cart = Cart.where(:consumer_id => session[:consumer_id]).first
@cart_items = CartItem.where(:cart_id => cart.id)
render 'show'
end
end
Vielen Dank im Voraus.
Können Sie bitte den Controllercode anzeigen? –
Ich hatte Controller-Code hinzugefügt. – Shubh
Gehen Sie durch diese Antwort https://stackoverflow.com/questions/19560881/how-to-pass-a-value-to-rails-model-method-without-submit-form –