Ruby on rails - 回形针未保存到数据库 [英] Ruby on rails - paperclip not saving to database
问题描述
我正在尝试在 Rails 中创建产品页面.这包括添加多个图像.我有一种用于产品的模型,一种用于照片和用户.我正在使用回形针宝石上传照片.但我有两个问题.
I'm trying to make a create product page in rails. This includes adding multiple images. I have one model for products one for photos and for users. I'm using the paperclip gem for photo upload. But I have 2 problems.
- 我的文件输入不允许允许我选择多个图像
- 当我查看产品时没有图片显示,因为图片没有保存到数据库
附言我使用 HAML,但没有照片控制器.
P.S. I use HAML and I dont have a photo controller.
产品控制器
class ProductsController < ApplicationController
before_filter :current_user, only: [:create, :destory]
before_filter :correct_user, only: :destory
def new
@product = Product.new
@photo = Photo.new
5.times { @product.photos.build }
end
def create
@photo = current_user.photos.build(params[:photo])
@product = current_user.products.build(params[:product])
if @product.save
render "show", :notice => "Sale created!"
else
render "new", :notice => "Somehting went wrong!"
end
end
def show
@product = Product.find(params[:id])
end
创建产品页面
= form_for @product, :html => { :multipart => true } do |f|
- if @product.errors.any?
.error_messages
%h2 Form is invalid
%ul
- for message in @product.errors.full_messages
%li
= message
%p
= f.label :name
= f.text_field :name
%p
= fields_for :photos do |f_i|
=f_i.file_field :image
%p.button
= f.submit
产品型号
class Product < ActiveRecord::Base
attr_accessible :description, :name, :price, :condition, :ship_method, :ship_price, :quantity, :photo
has_many :photos, dependent: :destroy
accepts_nested_attributes_for :photos
belongs_to :user
照片模型
class Photo < ActiveRecord::Base
attr_accessible :product_id
belongs_to :product
has_attached_file :image,
:styles => {
:thumb=> "100x100#",
:small => "300x300>",
:large => "600x600>"
}
end
用户模型
class User < ActiveRecord::Base
attr_accessible :email, :password, :password_confirmation, :name
attr_accessor :password
has_many :products, dependent: :destroy
has_many :photos,:through=>:products
显示产品页面
%b seller
= @product.user.name
%br
- @product.photos.each do |photo|
= image_tag photo.image.url
推荐答案
您是否使用 Resque
作为后台作业,如果是,那么您需要使用 rake resque:work QUEUE= 启动它'*'
.in rails 通常 Resque
用于处理涉及邮寄和图片上传的后台作业.使用回形针配置 Amazon S3.
Are you using Resque
for background job,if yes then u need to start it using rake resque:work QUEUE='*'
.in rails usually Resque
is used to handle background jobs which involves mailer and picture upload.OR an example below for product.html.erb having partial for photos upload for that product with paperclip configures with Amazon S3.
product.html.erb
product.html.erb
<%= render :partial => 'photos' %>
_photos.html.erb 至少需要一张图片
_photos.html.erb for atleast one image mandatory
<% if @product.photos[0].nil? %>
<a href="javascript:void(0);" class="add-photos" >
<img src="/assets/default/product-add-photos.png" alt="Add product photos"/>
</a>
<% end %>
<img src="<%= (@product.product_photos[0].nil? ? "" : @product.photos[0].image.url(:small)) %>" id="photos_1" class="product-photos-src <%=@product.photos[0].nil? ? 'dontdisplay' : ''%> "/>
这篇关于Ruby on rails - 回形针未保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!