2017-07-23 4 views
0

Ich habe, was eine ziemlich einfache Einrichtung für Büroklammer auf meiner Schiene App sein sollte.Schienen: Büroklammer speichert das Bild nicht in DB

Mein products_controller.rb ist dies:

class ProductsController < ApplicationController 
    ... 

    def new 
    @product = Product.new 
    end 

    def create 
    @product = Product.new(product_params) 
    @product.save 
    flash[:notice] = "Your product has been created!" 
    redirect_to products_path 
    end 

    def edit 
    @product = Product.find(params[:id]) 
    end 

    def update 
    @product = Product.find(params[:id]) 
    if @product.update_attributes(product_params) 
     redirect_to products_path 
     flash[:notice] = "That product has been updated." 
    else 
     render :action => :edit 
     flash[:alert] = "Something went terribly wrong there..." 
    end 
    end 

    ... 

    private 

    def product_params 
    params.require(:product).permit(:name, :price, :active, :short_description, :weight, :box_length, :box_width, :box_depth, :product_image) 
    end 
end 

Meine products#edit Form (products#new funktioniert auch nicht, aber es ist die gleiche Form):

<%= form_for @product, html: { multipart: true } do |f| %> 

    <div class="form-inputs row"> 
    <div class="form-group col-xs-9"> 
     <%= f.label "Product Name" %> 
     <%= f.text_field :name, class: "form-control" %> 
    </div> <!-- form group --> 

    <div class="form-group col-xs-3"> 
     <%= f.label :price %> 
     <%= f.text_field :price, class: "form-control", data: {autonumeric: true} %> 
    </div> <!-- form group --> 

    <div class="form-group col-xs-12"> 
     <%= f.label "Product Description" %> 
     <%= f.text_area :short_description, class: "form-control" %> 
    </div> <!-- form group --> 

    <div class="form-group col-xs-12"> 
     <%= f.file_field :image, as: :file %> 
    </div> <!-- form group --> 

    <div class="form-group text-center col-xs-12"> 
     <p><%= f.check_box :active %> This is an active product.</p> 
    </div> <!-- form group --> 

    <div class="row"> 
     <hr class="col-xs-6 col-xs-push-3" /> 
    </div> 

    <div class="col-xs-12"> 
     <h2 class="text-center">Shipping Information</h2> 
    </div> 

    <div class="form-group col-xs-6"> 
     <%= f.label "Product Length (in Inches)" %> 
     <%= f.text_field :box_length, class: "form-control" %> 
    </div> <!-- form group --> 

    <div class="form-group col-xs-6"> 
     <%= f.label "Product Width (in Inches)" %> 
     <%= f.text_field :box_width, class: "form-control" %> 
    </div> <!-- form group --> 

    <div class="form-group col-xs-6"> 
     <%= f.label "Product Depth (in Inches)" %> 
     <%= f.text_field :box_depth, class: "form-control" %> 
    </div> <!-- form group --> 

    <div class="form-group col-xs-6"> 
     <%= f.label "Product Weight (in Pounds)" %> 
     <%= f.text_field :weight, class: "form-control" %> 
    </div> <!-- form group --> 
    </div> 

    <div class="form-actions text-center"> 
    <%= f.button :submit, class: "btn btn-manly" %> 
    </div> 
<% end %> 

Und meine relevanten Routen:

Ressourcen: Produkte

Und schließlich die product.rb Modell:

class Product < ActiveRecord::Base 
    has_many :order_items 
    has_attached_file :product_image 
    validates_attachment_content_type :product_image, content_type: /\Aimage\/.*\z/ 
    validates_attachment_file_name :product_image, matches: [/png\z/, /jpe?g\z/] 

    default_scope { where(active: true) } 
end 

Und die schema.rb für die Produkttabelle:

create_table "products", force: :cascade do |t| 
    t.string "name" 
    t.decimal "price",      precision: 12, scale: 3 
    t.boolean "active" 
    t.datetime "created_at",           null: false 
    t.datetime "updated_at",           null: false 
    t.string "short_description" 
    t.decimal "weight" 
    t.decimal "box_length" 
    t.decimal "box_width" 
    t.string "box_depth" 
    t.string "product_image_file_name" 
    t.string "product_image_content_type" 
    t.integer "product_image_file_size" 
    t.datetime "product_image_updated_at" 
    end 

Ich erhalte keine Fehler, wenn ich muss. Ich habe auch die und die form_for Variationen versucht, die in der Büroklammerdokumentation gegeben werden. Ich habe es überprüft und ImageMagick ist installiert. Kann jemand sehen, warum es nicht spart? Wenn ich die Konsole überprüfe, nachdem ich versucht habe, ein Bild hochzuladen, wird nur nil für alle vier Büroklammerfelder angezeigt.

+0

Was ist Ihr Modell aussehen hochladen? Haben Sie die Büroklammer richtig eingerichtet? Hatten Sie die richtigen Migrationen für Ihr Büroklammerfeld im Modell? – PressingOnAlways

+0

@PressingOnAlways, ich habe das Schema und das Modell zum OP hinzugefügt. – Liz

Antwort

1

Sie verwenden :image für file_field aber Ihre Büroklammer Feld product_image, ändern verwenden wie folgt Datei

<div class="form-group col-xs-12"> 
    <%= f.file_field :product_image, as: :file %> 
</div> <!-- form group --> 
Verwandte Themen