rails new ecommerce_store cd ecommerce_store
gem 'devise'
bundle install rails generate devise:install rails generate devise User rails db:migrate
root 'products#index'
gem 'stripe'
rails generate model Product title:string description:text price:decimal stock:integer rails db:migrate
class Product < ApplicationRecord validates :title, :price, :stock, presence: true end
rails generate controller Products
class ProductsController < ApplicationController
def index
@products = Product.all
end
def show
@product = Product.find(params[:id])
end
def new
@product = Product.new
end
def create
@product = Product.new(product_params)
if @product.save
redirect_to @product, notice: 'Product created successfully.'
else
render :new
end
end
private
def product_params
params.require(:product).permit(:title, :description, :price, :stock)
end
endresources :products
<h1>Products</h1>
<ul>
<% @products.each do |product| %>
<li><%= link_to product.title, product %> - <%= number_to_currency(product.price) %></li>
<% end %>
</ul>
<%= link_to 'New Product', new_product_path %><h1><%= @product.title %></h1> <p><%= @product.description %></p> <p>Price: <%= number_to_currency(@product.price) %></p>
rails generate controller Carts
class CartsController < ApplicationController
def show
@cart_items = current_cart
end
def add_item
product = Product.find(params[:product_id])
cart = current_cart
cart[product.id.to_s] ||= { 'quantity' => 0 }
cart[product.id.to_s]['quantity'] += 1
session[:cart] = cart
redirect_to cart_path
end
def remove_item
cart = current_cart
cart.delete(params[:product_id])
session[:cart] = cart
redirect_to cart_path
end
private
def current_cart
session[:cart] ||= {}
end
endresource :cart, only: [:show] do post 'add_item/:product_id', to: 'carts#add_item', as: :add_item delete 'remove_item/:product_id', to: 'carts#remove_item', as: :remove_item end
<h1>Your Cart</h1>
<% if @cart_items.any? %>
<% @cart_items.each do |product_id, details| %>
<% product = Product.find(product_id) %>
<div>
<h3><%= product.title %></h3>
<p>Quantity: <%= details['quantity'] %></p>
<p>Price: <%= number_to_currency(product.price * details['quantity']) %></p>
<%= button_to 'Remove', remove_item_cart_path(product_id: product.id), method: :delete %>
</div>
<% end %>
<% else %>
<p>Your cart is empty.</p>
<% end %><%= button_to 'Add to Cart', add_item_cart_path(product_id: @product.id), method: :post %>
stripe: publishable_key: pk_test_xxxxx secret_key: sk_test_xxxxx
rails generate controller Checkouts
class CheckoutsController < ApplicationController
def create
product = Product.find(params[:product_id])
@session = Stripe::Checkout::Session.create(
payment_method_types: ['card'],
line_items: [{
name: product.title,
amount: (product.price * 100).to_i,
currency: 'usd',
quantity: 1
}],
mode: 'payment',
success_url: checkout_success_url + "?session_id={CHECKOUT_SESSION_ID}",
cancel_url: checkout_cancel_url
)
respond_to do |format|
format.js # renders create.js.erb for Stripe redirection
end
end
def success
# Handle successful payment logic here
end
def cancel
# Handle canceled payment logic here
end
endpost 'checkout/create', to: 'checkouts#create' get 'checkout/success', to: 'checkouts#success' get 'checkout/cancel', to: 'checkouts#cancel'
var stripe = Stripe('<%= Rails.application.credentials[:stripe][:publishable_key] %>');
stripe.redirectToCheckout({ sessionId: '<%= @session.id %>' });
<%= button_to 'Checkout with Stripe', checkout_create_path(product_id: product.id), remote: true %>
rails active_storage:install rails db:migrate
class Product < ApplicationRecord has_one_attached :main_image has_many_attached :gallery_images validates :title, :price, :stock, presence: true end
<%= form_with(model: @product, local: true) do |form| %>
<div class="field">
<%= form.label :main_image %>
<%= form.file_field :main_image %>
</div>
<div class="field">
<%= form.label :gallery_images %>
<%= form.file_field :gallery_images, multiple: true %>
</div>
<% end %>Sign up for free and be the first to get notified about new posts.
No comment