2016-08-28 2 views
1

enter image description hereROR 오류를 '숫자가 아닌'

오류 메시지를 제공 number_field_tag ​​

enter image description here

내가 '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 

답변

0

 <%= f.label :stock_quantity %> 
     <%= f.number_field :stock_quantity, min: 1, class: 'form-control' %> 

사람은 'number_field_tag'가 작동하지 않는 이유를 가르치 려 수 있습니다 양을 교체하여 문제가이와 코드를 제출 olved?

+0

생성 된 HTML 코드를 보면 필드의 이름이 'name ='stock_quantity ''이지만 컨트롤러의 #product_params params.require (: product) .permit (: title, : 설명, : image, : price, : stock_quantity)는 명확한 설명을 위해 'name ='product [stock_quantity] '' – j2FunOnly

+0

을 가져 주셔서 감사합니다. 그것은 'f.number_field'가 똑같이 사용하기에 좋다는 것을 의미합니까? –

0

이 그것을 해결, html로 원문을 강조 이전 덧글에

<%= number_field_tag 'product[stockquantity]', min: 1, class:"form-control" %> 

감사합니다.

관련 문제