Posts

Remove Product From Cart

Remove Product From Cart Step1-add this link into index.html.erb-carts <%= link_to "Remove",  remove_cart_path(cart_item.product.id), data: {method: :delete, remote: true}  %> Step2-Create a new method in product controller.rb  def remove_cart     @product = Product.friendly.find(params[:id])     @remove_cart = current_cart.cart_items.where(product_id: @product.id).first.destroy     redirect_to "/carts"   end Step3-Create a Routes path delete 'remove_cart_item/:id' => "products#remove_cart", as: :remove_cart

Add to cart in Rails

ADD TO CART Step1.Add the link of add to cart <a href="/products/<%= product.id %>/cart"><i class="ion-android-cart"></i>   </a> Step 2.create a method in product controller--> def add_to_cart  @product = Product.find(params[:product_id])     if @product       if user_signed_in?         if current_cart.present?           cart_item = current_cart.cart_items.find_by_product_id(@product.id)           if cart_item.blank?             cart_item = current_cart.cart_items.new(product_id: @product.id)           end           cart_item.unit_price = @product.price           # cart_item.price = cart_item.unit_price * cart_item.quantity           cart_item.price = cart_item.unit_price * 1         ...

Friendly_id gem in Rails

What is Friendly_id gem? . FriendlyId  is the “Swiss Army bulldozer” of slugging and permalink plugins for ActiveRecord. It allows you to create pretty URL's and work with human- friendly strings as if they were numeric  ids  for ActiveRecord models.  Usage Add this line to your application's Gemfile: gem ' friendly_id ' , ' ~> 5.2.4 ' # Note: You MUST use 5.0.0 or greater for Rails 4.0+ And then execute: bundle install Add a  slug  column to the desired table (e.g.  Users ) rails g migration AddSlugToProducts slug:uniq Generate the friendly configuration file and a new migration rails generate friendly_id Note: You can delete the  CreateFriendlyIdSlugs  migration if you won't use the slug history feature. ( Read more ) Run the migration scripts rails db:migrate Edit the  app/models/product.rb  file as the following: class Product < ApplicationRecord extend FriendlyId f...

Rails Girls Guides

Image
Getting Started with Rails                              posted by-manishsingh27598@gmail.com 1.Add devise gem Open up your  Gemfile  and add this line gem 'devise' and run bundle install to install the gem.  Also remember to restart the Rails server 2. 2.Set up devise in your app Run the following command in the terminal. rails g devise:install 3. 3.Configure Devise Ensure you have defined default url options in your environments files. Open up  config/environments/development.rb  and add this line: config . action_mailer . default_url_options = { host: 'localhost' , port: 3000 } before the  end  keyword. Open up  app/views/layouts/application.html.erb  and add: <% if notice %> <p class= "alert alert-success" > <%= notice %> </p> <% end %> <% if alert %> ...