class Picture < ActiveRecord::Base
belongs_to :imageable, polymorphic: true
mount_uploader :file, PictureUploader
end
class PictureUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
class Ticket < ActiveRecord::Base
has_many :pictures, as: :imageable
accepts_nested_attributes_for :pictures
end
查看:
h1 Add a new Ticket
= form_for @ticket do |f|
= render partial: "fields", locals: {f: f}
= f.fields_for :picture do |p|
div
= p.label :file
= p.file_field :file, :multiple => true
div
= f.submit "Create a Ticket"
控制器:
class TicketsController < ApplicationController
def create
@ticket = Ticket.new(ticket_params)
t = Logger.new(STDOUT)
t.debug "================================"
t.debug ticket_params
t.debug "================================"
@ticket.init_sp(ticket_show_path(Ticket.generate_id), request)
@ticket.errors.add(:base, "Are You Spammer?") if @ticket.spam?
if @ticket.errors.empty? and @ticket.save
redirect_to ticket_show_path(@ticket.token), notice: "Ticket created successfully. Message sent."
else
render "new"
end
end
private
def load_ticket
@ticket = Ticket.find_by_token(params[:token])
end
def ticket_params
params.require(:ticket).permit(:customer_name, :customer_email, :subject, :body, :department_id, picture_attributes: [:file])
end
end
日志:
Started POST "/tickets" for 127.0.0.1 at 2014-06-14 16:22:28 +0300
Processing by TicketsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"3ToWruQi0Pgyo1wvRz9yOib5k13yV
Vs05dXAaWA4NuM=", "ticket"=>{"customer_name"=>"Alex Drobazko", "customer_email"=
>"drobazko@gmail.com", "subject"=>"Sample Subject", "department_id"=>"", "body"=
>"Sample Body", "picture"=>{"file"=>[#<ActionDispatch::Http::UploadedFile:0x34f6
5f0 @tempfile=#<File:C:/Users/drobazko/AppData/Local/Temp/RackMultipart20140614-
8144-hj7xsk>, @original_filename="28122007(001).jpg", @content_type="image/jpeg"
, @headers="Content-Disposition: form-data; name=\"ticket[picture][file][]\"; fi
lename=\"28122007(001).jpg\"\r\nContent-Type: image/jpeg\r\n">, #<ActionDispatch
::Http::UploadedFile:0x34f65d8 @tempfile=#<File:C:/Users/drobazko/AppData/Local/
Temp/RackMultipart20140614-8144-q2595k>, @original_filename="f3.jpg", @content_t
ype="image/jpeg", @headers="Content-Disposition: form-data; name=\"ticket[pictur
e][file][]\"; filename=\"f3.jpg\"\r\nContent-Type: image/jpeg\r\n">]}}, "commit"
=>"Create a Ticket"}
Unpermitted parameters: picture
D, [2014-06-14T16:22:28.099362 #8144] DEBUG -- : ===============================
=
Unpermitted parameters: picture
D, [2014-06-14T16:22:28.103362 #8144] DEBUG -- : {"customer_name"=>"Alex Drobazk
o", "customer_email"=>"drobazko@gmail.com", "subject"=>"Sample Subject", "body"=
>"Sample Body", "department_id"=>""}
D, [2014-06-14T16:22:28.107362 #8144] DEBUG -- : ===============================
=
问题是在Picture表中没有写入,但它在Ticket表中写入(请参阅日志)。 有什么建议吗?
编辑:当我尝试将视图 = f.fields_for:图片执行| p | 更改为 = f.fields_for:图片执行| p | 但是它给了我错了未初始化的常量Ticket :: Picture
另外,我试图改变:
params.require(:ticket).permit(:customer_name,:customer_email,:subject,:body,:department_id,picture_attributes:[:file])
到
params.require(:ticket).permit(:customer_name,:customer_email,:subject,:body,:department_id,pictures_attributes:[:file])(单数复数形式)但没有效果。
有趣的问题:
一旦我重新启动服务器并将其放入视图中:
= f.fields_for:图片做| p |
文件按钮在表格上消失了))
答案 0 :(得分:2)
您有 has_many pictures
。因此 ticket_params
应该是这样的
def ticket_params
params.require(:ticket).permit(:customer_name, :customer_email, :subject, :body, :department_id, pictures_attributes: [:file])
end
请注意 pictures
中的复数 pictures_attributes
。
<强>更新强>
还有这一行
= f.fields_for :picture do |p|
应该是这样的
= f.fields_for :pictures do |p|