Я пытаюсь сделать корзину товара. Я хочу иметь возможность выбирать количество продукта при добавлении продукта в корзину.
Это LineItemsController
class LineItemsController < ApplicationController
include CurrentCart
before_action :set_line_item, only: [:show, :edit, :update, :destroy]
before_action :set_cart, only: [:create]
def index
@line_items = LineItem.all
end
def show
end
def new
@line_item = LineItem.new
end
def edit
end
def create
product = Product.find(params[:product_id])
@line_item = @cart.add_product(product)
@line_item.quantity = params['q'].to_i
if @line_item.save
redirect_to @line_item.cart, notice: 'Item added to cart.'
else
render :new
end
end
def update
if @line_item.update(line_item_params)
redirect_to @line_item, notice: 'Line item was successfully updated.'
else
render :edit
end
end
def destroy
@cart = Cart.find(session[:cart_id])
@line_item.destroy
redirect_to cart_path(@cart), notice: 'Item successfully removed.'
end
private
def set_line_item
@line_item = LineItem.find(params[:id])
end
def line_item_params
params.require(:line_item).permit(:product_id, :quantity)
end
end
Это view / line_item / _form. html .erb
<%= simple_form_for(@line_item) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.association :product %>
<%= f.association :cart %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
Это view / product / show. html .erb
<% content_for :body_class, 'bg-light' %>
<section class="section instrument-show">
<div class="columns">
<div class="column is-8">
<%= render 'shared/breadcrumb', category: @product.category %>
<h1 class="title is-2"><%= @product.title %></h1>
<!--ul class="pv1">
<%# if @product.brand? %>
<li class="inline-block pr3"><%#= @product.brand %></li-->
<%# end %>
<!--li class="inline-block pr3"><%#= @product.model %></li-->
<%# if @product.condition? %>
<!--li class="inline-block pr3"><%#= @product.condition %></li>
<%# end %>
</ul-->
<div class="feature-image">
<%= image_tag(@product.image_url(:default).to_s) %>
</div>
<div class="content pa4 mt3 bg-white border-radius-3">
<h3 class="subtitle is-4">Description</h3>
<%= @product.description %>
<h3 class="subtitle is-4 pt5">Product Specs</h3>
<table class="table is-narrow">
<% if @product.category.present? %>
<tr>
<td class="has-text-weight-bold">Category:</td>
<td><%= link_to @product.category.name, @product.category %></td>
</tr>
<% end %>
<%# if @product.finish %>
<tr>
<td class="has-text-weight-bold">Finish:</td>
<td><%#= @product.finish %></td>
</tr>
<%# end %>
<tr>
<td class="has-text-weight-bold">Model:</td>
<td><%#= @product.model %></td>
</tr>
</table>
</div>
</div>
<div class="column is-3 is-offset-1">
<div class="bg-white pa4 border-radius-3">
<h4 class="title is-5 has-text-centered"><%= number_to_currency(@product.price) %></h4>
<p class="has-text-centered mb4">Sold by <%#= @product.user.name %></p>
<form>
<input type="number" name="q" min="1" max="50" step="1" class="input label">
</form>
<%= button_to 'Add to cart', line_items_path(product_id: @product), class: 'button is-warning add-to-cart' %>
</div>
</div>
</div>
<% if user_signed_in? && current_user.admin? %>
<%= link_to 'Edit', edit_admin_product_path(@product), class: 'button' %>
<% end %>
</section>
Теперь в этой строке @line_item.quantity = params['q'].to_i
from
<form>
<input type="number" name="q" min="1" max="50" step="1" class="input label">
</form>
я получаю "nil", и я отчаянно не знаю, что мне делать, чтобы получить фактическое число.
PS Это view / line_items / new. html .erb
<h1>New Line Item</h1>
<%= render 'form', line_item: @line_item %>
<%= link_to 'Back', line_items_path %>