How to pass value from input type in html to a controller in rails -
i beginner in html , ruby on rails. sounds basic question after many attempts not getting how pass value input type not in form controller.
this 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 cart', add_to_cart_path(:product_id => product.id, :price => product.price, :quantity => 'quantity') %></button> <% end%> <br> </div>
this controller
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
thanks in advance.
you either need use form
post data, or need use asynchronous javascript request post data.
Comments
Post a Comment