This is my first cart implementation in rails so i wanted to get some feedback on the right method to implement it.
First of all i am storing the data directly in session because the items in the cart are small.
The idea is that i save a list of integers(ids of the items) in session. When a user clicks on “Add to Cart” then the id of that product will be added to the list stored in the rails session variable.
class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception before_action :set_cart private def set_cart if session[:cart] @cart = session[:cart] else session[:cart] =  @cart = session[:cart] end end end
Main index file lists entries with “Add to Cart” button for each entry. After the user clicks on “Add to Cart” then add_item method of CartController is called.
<ol> <% @karaokes.each do |karaoke| %> <li> <%= link_to karaoke.name, karaoke_path(karaoke) %> <%= link_to "Add to Cart", add_path(karaoke) %> </li> <% end %> </ol>
Cart Controller looks as follows:
class CartController < ApplicationController def add_item k = Karaoke.find(params[:id]) if !@cart.include?(k.id) @cart.push(k.id) end redirect_to cart_path end def list_items @items = @cart end def remove_item @cart.delete_at(params[:index].to_i) redirect_to cart_path end end
Finally the cart page looks as follows which lists all the items added to cart.
<h1> Cart </h1> <% @items.each_with_index do |itm, index| %> <p> <%= Karaoke.find(itm).name %> <%= link_to "Remove", remove_path(index) %> </p> <% end %>
I appreciate any feedback on this implementation. Thanks!
✓ Extra quality
ExtraProxies brings the best proxy quality for you with our private and reliable proxies
✓ Extra anonymity
Top level of anonymity and 100% safe proxies – this is what you get with every proxy package
✓ Extra speed
1,ooo mb/s proxy servers speed – we are way better than others – just enjoy our proxies!
USA proxy location
We offer premium quality USA private proxies – the most essential proxies you can ever want from USA
Our proxies have TOP level of anonymity + Elite quality, so you are always safe and secure with your proxies
Use your proxies as much as you want – we have no limits for data transfer and bandwidth, unlimited usage!
Superb fast proxy servers with 1,000 mb/s speed – sit back and enjoy your lightning fast private proxies!
99,9% servers uptime
Alive and working proxies all the time – we are taking care of our servers so you can use them without any problems
No usage restrictions
You have freedom to use your proxies with every software, browser or website you want without restrictions
Perfect for SEO
We are 100% friendly with all SEO tasks as well as internet marketing – feel the power with our proxies
Buy more proxies and get better price – we offer various proxy packages with great deals and discounts
We are working 24/7 to bring the best proxy experience for you – we are glad to help and assist you!