오류 메시지를 제공 number_field_tag
내가 'number_field_tag'을 통해 숫자 값을 삽입하지만 같은 오류 메시지가 계속하려고 해요 . 누구든지이 원인을 알고 있습니까?
new.html.erb
<% provide(:title, 'Products') %>
<h1>Products</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= render 'shared/error_messages_p' %>
<%= form_for([:admin, @product], :html => {multipart:true}) do |f| %>
<%= f.label :title %>
<%= f.text_field :title, class: 'form-control' %>
<%= f.label :description %>
<%= f.text_area :description, :rows => 6, class: 'form-control' %>
<%= f.file_field :image %>
<%= number_field_tag :stock_quantity, 1, min: 1, class: "form-control" %>
<%= f.label :price %>
<%= f.text_field :price, class: 'form-control' %>
<%= f.submit "Create product", class: "btn btn-primary" %>
<% end %>
</div>
</div>
products_controller.rb
class Admin::ProductsController < Admin::BaseController
before_action :set_product, only: [:show, :edit, :update, :destroy]
rescue_from ActiveRecord::RecordNotFound, with: :invalid_product
def index
@products = Product.all
end
def show
@product = Product.find(params[:id])
end
def new
@product = Product.new
end
def edit
end
def create
@product = Product.new(product_params)
if @product.save
redirect_to [:admin, @product], notice: 'Product was successfully created.'
else
render :new
end
end
def update
if @product.update(product_params)
redirect_to @product, notice: 'Product was successfully updated.'
else
render :edit
end
end
def destroy
@product.destroy
redirect_to products_url, notice: 'Product was successfully destroyed.'
end
private
def set_product
@product = Product.find(params[:id])
end
def product_params
params.require(:product).permit(:title, :description, :image, :price, :stock_quantity)
end
def invalid_product
logger.error "Attempt to access invalid cart #{params[:id]}"
redirect_to products_url, notice: 'Invalid product'
end
end
제품 모델
class Product < ActiveRecord::Base
attr_accessor :image
validates :title, :description, :image, presence: true
validates :price, numericality: {greater_than_or_equal_to: 0.01}
validates :title, uniqueness: true
validates :stock_quantity, numericality: { only_integer: true}
validates :image, allow_blank: true, format: {with: %r{\.(gif|jpg|png)\Z}i, message: 'must be a URL for GIF, JPG or PNG image.'}
mount_uploader :image, ImageUploader
생성 된 HTML 코드를 보면 필드의 이름이 'name ='stock_quantity ''이지만 컨트롤러의 #product_params params.require (: product) .permit (: title, : 설명, : image, : price, : stock_quantity)는 명확한 설명을 위해 'name ='product [stock_quantity] '' – j2FunOnly
을 가져 주셔서 감사합니다. 그것은 'f.number_field'가 똑같이 사용하기에 좋다는 것을 의미합니까? –