我正在嘗試在rails中創建產品頁面。這包括添加多個圖像和文本字段。我有一個產品模型和一個照片模型。我正在使用回形針創建照片上傳。 但是當我查看產品頁面時,我沒有得到任何圖片。照片沒有被保存到數據庫。Rails - 回形針 - 多張照片上傳不保存
P.S.我使用HAML。
應用程序/視圖/產品/ show.html.haml
%b Name
= @product.name
%br
%b Description
= @product.description
%br
- @product.photos.each do |photo|
= image_tag photo.image.url
應用程序/控制器/ products_controller
class ProductsController < ApplicationController
before_filter :require_login
before_filter :current_user, only: [:create, :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
應用程序/模型/攝
class Photo < ActiveRecord::Base
attr_accessible :product_id
belongs_to :product
has_attached_file :image,
:styles => {
:thumb=> "100x100#",
:small => "300x300>",
:large => "600x600>"
}
end
應用程序/模型/產品
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
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
應用/產品/ new.html.haml
刪除此行** has_attached_file:您的產品模型中的照片**在此處不需要。 – 2013-05-23 07:39:10
我到目前爲止還不明白爲什麼你又一次提出同樣的問題......老一代呢?你正在浪費別人的時間.. http://stackoverflow.com/questions/16665809/ruby-on-rails-paperclip-not-saving-to-database – 2013-05-30 08:41:18
和第三個是在這裏 http://stackoverflow.com/問題/ 16807112/rails-paperclip-is-not-adding-my-image – 2013-05-30 08:47:48