【发布时间】:2014-04-13 05:36:23
【问题描述】:
我尝试使用回形针 gem 将附件添加到我的联系人模型中。看起来我可以添加附件 - 我可以毫无问题地选择文件。但是,当我创建联系人时,附件不会保存。我知道这一点是因为我可以在 rails 控制台中看到 document_id 为“nil”。
在用于创建新联系人的 _form.html.erb 中,我有:
<%= simple_form_for(@contact, html: {class: "form-inline well", multipart: true}, role: "form") do |f| %>
<% if @contact.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@contact.errors.count, "error") %> prohibited this contact from being saved:</h2>
<ul>
<% @contact.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.input :name %>
<%= f.input :category %>
<%= f.input :area %>
<%= f.input :phone %>
<%= f.input :website %>
<%= f.input :email %>
<%= f.fields_for :document do |document_fields| %>
<%= document_fields.input :attachment, as: :file %>
<% end %>
<%= f.button :submit, class: "btn-success" %>
</div>
<% end %>
我的数据库迁移是:
class CreateDocuments < ActiveRecord::Migration
def change
create_table :documents do |t|
t.integer :user_id
t.timestamps
end
add_index :documents, :user_id
add_attachment :documents, :add_attachment
add_column :contacts, :document_id, :integer
end
end
我的 document.rb 模型是:
class Document < ActiveRecord::Base
has_attached_file :attachment, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :attachment, :content_type => /\Aimage\/.*\Z/
end
对于contact.rb 是:
class Contact < ActiveRecord::Base
belongs_to :user
belongs_to :document
accepts_nested_attributes_for :document
validates :name, presence: true,
length: { minimum: 2}
validates :user_id, presence: true
end
我在 contacts_controller.rb 中的操作是:
def create
@contact = current_user.contacts.new(contact_params)
respond_to do |format|
if @contact.save
format.html { redirect_to @contact, notice: 'Contact was successfully created.' }
format.json { render action: 'show', status: :created, location: @contact }
else
format.html { render action: 'new' }
format.json { render json: @contact.errors, status: :unprocessable_entity }
end
end
end
def contact_params
params.require(:contact).permit(:name, :email, :category, :area, :organisation, :website, :phone, :user_id, document_attributes: [:id, :attachment])
end
# PATCH/PUT /contacts/1
# PATCH/PUT /contacts/1.json
def update
@contact = current_user.contacts.find(params[:id])
if params[:contact] && params[:contact].has_key?(:user_id)
params[:contact].delete(:user_id)
end
respond_to do |format|
if @contact.update((contact_params))
format.html { redirect_to @contact, notice: 'Contact was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @contact.errors, status: :unprocessable_entity }
end
end
end
当我尝试使用附件创建新联系人时,这是我的 heroku 日志:
2014-03-10T23:41:49.594786+00:00 app[web.1]:参数: {"utf8"=>"✓", "authenticity_token"=>"FGitLGkHzfw15yksCusGDvwdb//CgMyOMFh4SS4l63Y=", "联系人"=>{"姓名"=>"Oliver OT", "category"=>"OT", "area"=>"Rockdale", "电话"=>"", "网站"=>"", "电子邮件"=>"", "document_attributes"=>{"附件"=>#, @original_filename="2014-03-03 晚上 9.24.40 截屏.png", @content_type="image/png", @headers="Content-Disposition: form-data; 名称=\“联系人[文档属性][附件]\”;文件名=\“屏幕 于 2014 年 3 月 3 日晚上 9.24.40 拍摄.png\"\r\n内容类型: image/png\r\n">}}, "commit"=>"创建联系人"} 2014-03-10T23:41:49.594786+00:00 应用 [web.1]:参数: {"utf8"=>"✓", "authenticity_token"=>"FGitLGkHzfw15yksCusGDvwdb//CgMyOMFh4SS4l63Y=", "联系人"=>{"姓名"=>"Oliver OT", "category"=>"OT", "area"=>"Rockdale", "电话"=>"", "网站"=>"", "电子邮件"=>"", "document_attributes"=>{"附件"=>#, @original_filename="2014-03-03 晚上 9.24.40 截屏.png", @content_type="image/png", @headers="Content-Disposition: form-data; 名称=\“联系人[文档属性][附件]\”;文件名=\“屏幕 于 2014 年 3 月 3 日晚上 9.24.40 拍摄.png\"\r\n内容类型: image/png\r\n">}}, "commit"=>"创建联系人"}
我不确定如何正确保存附件?
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 paperclip