[英]Rails: Paperclip Not Saving Image to DB
I have what should be a fairly simple setup for paperclip on my rails app. 我在我的rails应用程序上应该有一个相当简单的回形针设置。
My products_controller.rb
is this: 我的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
My products#edit
form ( products#new
doesn't work either, but it's the same form): 我的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 %>
And my relevant routes: 和我相关的路线:
resources :products 资源:产品
And finally the product.rb
model: 最后是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
And the schema.rb
for the products table: 产品表的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
I am getting no errors when I submit. 提交时我没有收到任何错误。 I have also tried both the simple_form_for
and the form_for
variations given in the Paperclip documentation. 我还尝试了Paperclip文档中提供的simple_form_for
和form_for
变体。 I have double checked and ImageMagick is installed. 我已仔细检查并安装了ImageMagick。 Can anyone see why it isn't saving? 谁能看到为什么它没有保存? When I check the console after trying to upload an image it just says nil
for all four paperclip fields. 当我尝试上载图像后检查控制台时,它对所有四个回形针字段都说nil
。
You are using :image
for file_field
but your paperclip field is using product_image
, change as follow to upload file 您正在使用:image
作为file_field
但您的回形针字段正在使用product_image
,请按照以下说明进行更改以上传文件
<div class="form-group col-xs-12">
<%= f.file_field :product_image, as: :file %>
</div> <!-- form group -->
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.