0
我有什麼應該是一個相當簡單的安裝回形針在我的Rails應用程序。Rails:回形針不保存圖像到DB
我products_controller.rb
是這樣的:
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
我products#edit
形式(products#new
也不管用,但它是相同的形式):
<%= 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 %>
而且我的相關路線:
資源:產品
最後product.rb
型號:
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
而且schema.rb
的產品表:
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
我沒有得到任何錯誤,當我提交。我也嘗試了Paperclip文檔中給出的simple_form_for
和form_for
變體。我有雙重檢查和ImageMagick安裝。任何人都可以看到它爲什麼沒有保存?當我試圖上傳一張圖片後檢查控制檯時,它只對所有四個回形針字段說nil
。
什麼是你的模型是什麼樣子?你是否正確設置了回形針?您在模型中的回形針領域是否有適當的遷移? – PressingOnAlways
@PressingOnAlways,我將模式和模型添加到OP。 – Liz