我创建了一个表单,用户可以在其中上传文件: :File
是我的列Setting model
<%= form_for(@setting) do |f| %>
<div class="field">
<%= f.label 'Patientendaten' %>
<%= f.file_field :file %>
</div>
.......
当我不上传文件而只是编辑其他“设置”时没有问题。但是当我尝试上传文件时,我收到了这个错误:
NoMethodError in SettingsController#update
undefined method `name' for nil:NilClass
app/controllers/settings_controller.rb:72:in `block in update'
app/controllers/settings_controller.rb:71:in `update'
对应的参数是:
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"eFchgMpE0A46jQI0asmiR2wH+4kq/vmSzDchlBmMJaA=",
"setting"=>{"adobe"=>"",
"file"=>#<ActionDispatch::Http::UploadedFile:0x3764b98 @original_filename="prawn.rb",
@content_type="application/octet-stream",
@headers="Content-Disposition: form-data; name=\"setting[file]\"; filename=\"prawn.rb\"\r\nContent-Type: application/octet-stream\r\n",
@tempfile=#<File:C:/Users/STADLE~1/AppData/Local/Temp/RackMultipart20130919-6776- 1bs1n2d>>,
"treiber"=>"",
"port"=>"",
"druckername"=>"",
"bild"=>"C:/geburtstag/app/vorderseite/default.jpg",
"anrede"=>"",
"text"=>""},
"commit"=>"Speichern",
"id"=>"1"}
我想错误是由于参数而发生的?
如果您想查看我的控制器:
def update
@setting = Setting.find(params[:id])
respond_to do |format|
if @setting.update_attributes(params[:setting])
pdf = Prawn::Document.new(:page_size=> "A4",:margin => 0)
pdf.image @setting.bild ,:at => [0, Prawn::Document::PageGeometry::SIZES["A4"][1]],:fit => Prawn::Document::PageGeometry::SIZES["A4"]
Dir.chdir Rails.root.to_s + '/vorderseite' do |dir|
pdf.render_file "vorderseite.pdf"
end
format.html { redirect_to patients_path, notice: 'Setting was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @setting.errors, status: :unprocessable_entity }
end
end
end
模型:
class Setting < ActiveRecord::Base
attr_accessible :anrede, :text, :adobe , :bild, :treiber, :port, :druckername, :file
before_save :default_values
def default_values
self.bild = Rails.root.to_s + '/vorderseite/default.jpg' if self.bild.blank?
end
end