I’m new to rails. I need help. I’ve created a form that allows the user to customize an item and click a “submit” button. The “submit” button should redirect user to cart page and add the customized item to cart. However, when the “submit” button is clicked, it directs the user to cart page but does not add the item to cart. I’m stuck and am in need of some assistance.
Customs Controller
before_action :authenticate_user!, except: [:index]
def new
@prebuilts = Prebuilt.all
@custom = Custom.new
end
def create
@custom = Custom.new(params.require(:custom).permit(:built_in_game, :color, :crayon_walls, :entertainment_tablets, :manufacturer, :price, :size))
if @custom.save
flash[:success] = "new custom jet created"
redirect_to cart_url
else
flash.now[:error] = 'custom jet creation failed'
render :new, status: :unprocessable_entity
end
end
Cart Controller
def index
@cart = Prebuilt.all
render :index
end
def show
@cart = current_cart
end
def add_to_cart
current_cart.add_item(params[:prebuilt_id])
redirect_to cart_path(current_cart.id)
flash[:success] = 'Item successfully updated!'
end
Customs Form Page
before_action :authenticate_user!, except: [:index]
def new
@prebuilts = Prebuilt.all
@custom = Custom.new
end
def create
@custom = Custom.new(params.require(:custom).permit(:built_in_game, :color, :crayon_walls, :entertainment_tablets, :manufacturer, :price, :size))
if @custom.save
flash[:success] = "new custom jet created"
redirect_to cart_url
else
flash.now[:error] = 'custom jet creation failed'
render :new, status: :unprocessable_entity
end
end
Cart Page
<% content_for(:html_title) { 'Junior Jets | Cart' } %>
<h1 style="font-family:'Courier New'"><b>YOUR CART</b></h1>
<div class="cart">
<style>
#right {
text-align: right;
background-color: #ffffff;
}
</style>
<table id="line_items">
<tr>
<th>Product</th>
<th>Qty</th>
<th class="price">Unit Price</th>
<th class="price">Full Price</th>
</tr>
<% for item in @cart %>
<tr class="<%= cycle :odd, :even %>
<td><%=h item.name %></td>
</tr>
<% end %>
</tr>
</table>
<div id="right">
<div class="totals">
<div class="totals-item">
<label>Subtotal</label>
<div class="totals-value" id="cart-subtotal">71.97</div>
</div>
<div class="totals-item">
<label>Tax (10%)</label>
<div class="totals-value" id="cart-tax">3.60</div>
</div>
<div class="totals-item">
<label>Shipping</label>
<div class="totals-value" id="cart-shipping">15.00</div>
</div>
<div class="totals-item totals-item-total">
<label>Grand Total</label>
<div class="totals-value" id="cart-total">90.57</div>
</div>
</div>
<a href="http://localhost:3000/checkout">
<button>Checkout</button>
</a>
</div>
</div>
Routes
get 'cart', to: 'cart#index', as: 'cart';
post '/add_to_cart/:prebuilt_id' => 'cart#add_to_cart', :as => 'add_to_cart'
get 'checkout', to: 'pages#checkout', as: 'checkout';
get 'confirm', to: 'pages#confirm', as: 'confirm';
########################################################################################################
# root to: redirect('/jets')
get 'jets', to: 'jets#index', as: 'jets'
post 'jets', to: 'jets#create'
get 'jets/new', to: 'jets#new', as: 'new_jet'
get 'jets/:id/edit', to: 'jets#edit', as: 'edit_jet'
get 'jets/:id', to: 'jets#show', as: 'jet'
patch 'jets/:id', to: 'jets#update'
delete 'jets/:id', to: 'jets#destroy'
get 'jets/:jet_id/products', to: 'products#index', as: 'jet_products'
post 'jets/:jet_id/products', to: 'products#create'
get 'jets/:jet_id/products/new', to: 'products#new', as: 'new_jet_product'
get '/jets/:jet_id/products/:id/edit', to: 'products#edit', as: 'edit_jet_product'
get '/jets/:jet_id/products/:id', to: 'products#show', as: 'jet_product'
patch '/jets/:jet_id/products/:id', to: 'products#update'
delete '/jets/:jet_id/products/:id', to: 'products#destroy'
# get 'jets/:jet_id/reviews', to: 'reviews#index', as: 'jet_reviews'
# post 'jets/:jet_id/reviews', to: 'reviews#create'
# get 'jets/:jet_id/reviews/new', to: 'reviews#new', as: 'new_jet_review'
# get '/jets/:jet_id/reviews/:id/edit', to: 'reviews#edit', as: 'edit_jet_review'
# get '/jets/:jet_id/reviews/:id', to: 'reviews#show', as: 'jet_review'
# patch '/jets/:jet_id/reviews/:id', to: 'reviews#update'
# delete '/jets/:jet_id/reviews/:id', to: 'reviews#destroy'
get 'prebuilts', to: 'prebuilts#index', as: 'prebuilts'
get 'prebuilts/:id', to: 'prebuilts#show', as: 'prebuilt'
get 'customs/new', to: 'customs#new', as: 'new_custom'
post 'custom', to: 'customs#create'
user24686287 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.