我在试着做一个产品推车。我希望当我将产品添加到购物车时,能够选择产品的数量。
这是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来自
<form>
<input type="number" name="q" min="1" max="50" step="1" class="input label">
</form>我得到了“零”,我现在非常不知道我应该怎么做才能得到实际的数字。
附注:这是view/line_item/new.html.erb
<h1>New Line Item</h1>
<%= render 'form', line_item: @line_item %>
<%= link_to 'Back', line_items_path %>发布于 2020-05-27 20:47:20
new.html.erb是输入你的数据,如果你提交了,数据就会流动到你的控制器里面去创建方法,同时show只是展示数据,你不能在show里面输入表单,我建议试着把new.html.erb的q部分放到simple_form_for标签里面,因为它是数字,你可以使用number_field rails tag helper作为示例,名字:q,范围在1之间。50步骤1,下面是可能对您的问题有所帮助的示例
当您从new.html.erb提交时,您将收到params:q作为参数的一部分,并使用create方法保存它
<%= simple_form_for(@line_item) do |f| %>
<%= f.error_notification %>
<%= number_field(:q, in: 1.0..50.0, step: 1) %>
<div class="form-inputs">
<%= f.association :product %>
<%= f.association :cart %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>https://stackoverflow.com/questions/62040297
复制相似问题