2016-08-28 18 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'不工作?

+0

如果您查看生成的HTML代碼,您會看到該字段的名稱:name ='stock_quantity'',但控制器的#product_params params.require(:product).permit(:title,:description, :image,:price,:stock_quantity)期望'name ='product [stock_quantity]'' – j2FunOnly

+0

感謝您的澄清。這是否意味着'f.number_field'同樣好用? –

0

這個解決它,

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

感謝以前的評議突出HTML的字眼。