我有......
/config/routes.rb :
resources :surveys do
resources :screenshots
end
/app/models/survey.rb :
has_many :screenshots
/app/models/screenshot.rb :
attr_accessible :name, :shot, :screenshot, :result_ids
belongs_to :survey
has_many :results
has_attached_file :shot, :default_url => ActionController::Base.helpers.asset_path('missing_:style.png')
/db/schema.rb :
create_table "screenshots", :force => true do |t|
t.integer "survey_id"
t.boolean "resetting_cache", :default => false
t.string "url"
t.string "shot_file_name"
t.string "shot_content_type"
t.integer "shot_file_size"
t.datetime "shot_updated_at"
t.boolean "include", :default => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "name"
end
/app/controllers/screenshots_controller.rb :
def new
@survey = Survey.find(params[:survey_id])
@competitor = @survey.competitor
@screenshot = Screenshot.new
end
def create
@survey = Survey.find(params[:survey_id])
@screenshot = Screenshot.new(params[:screenshot])
@competitor = @survey.competitor
flash[:notice] = "Screenshot was successfully added." if @screenshot.save
respond_with(@survey, @screenshot)
end
/app/views/screenshots/_form.html.haml :
= simple_form_for [@survey, @screenshot] do |f|
= f.error_notification
= f.file_field :shot
= f.association :results, :collection => @survey.selected_results
= f.button :submit, :label => "Save", :id => "submit_screenshot"
有谁知道@screenshot = Screenshot.new(params[:screenshot])
为什么会给我unknown attribute: screenshot
错误?
此时的典型params
将为:
=> {"utf8"=>"✓",
"authenticity_token"=>"t33SO+/mpPlQY/+7+5iRTe6O2zL/MtqisYXyghzkLY8=",
"screenshot"=>
{"screenshot"=>
#<ActionDispatch::Http::UploadedFile:0x007fd5cca0ace0
@content_type="image/png",
@headers=
"Content-Disposition: form-data; name=\"screenshot[screenshot]\"; filename=\"youtube.png\"\r\nContent-Type: image/png\r\n",
@original_filename="youtube.png",
@tempfile=
#<File:/var/folders/k9/vnpft_6d7qs6xmdb9_4svvmw0000gn/T/RackMultipart20131125-3030-1gmx1sy>>,
"result_ids"=>["", "29857"]},
"commit"=>"Create Screenshot",
"action"=>"create",
"controller"=>"screenshots",
"survey_id"=>"14564"}
答案 0 :(得分:1)
"screenshot"=> #<ActionDispatch::Http::UploadedFile:0x007fd5cca0ace0>
此参数应调用shot
,而不是screenshot
。 shot
是您在模型中为附件命名的内容。
答案 1 :(得分:0)
在屏幕截图模型的attr_accessible中,您包括:screenshot。由于这是模型的名称而不是模型中的属性,因此您无需将其包含在attr_accessible中。这可能就是把它扔掉了。
另外,你的意思是你的文件名是models / screenshot.rb,而不是模特/截图吗?