2013-11-25 37 views
1

我有...爲什麼回形針給我這個「未知屬性」錯誤?

/config/routes.rb

resources :surveys do 
    resources :screenshots 
end 

/app/models/survey.rb

has_many :screenshots 

/應用/型號/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
"screenshot"=> #<ActionDispatch::Http::UploadedFile:0x007fd5cca0ace0> 

此參數應該叫shot,不screenshotshot是您在模型中命名的附件。

+0

thx - 修復它 –

0

在你attr_accessible的截圖模型,包括:屏幕截圖。由於這是模型的名稱而不是模型中的屬性,因此不需要將其包含在attr_accessible中。這可能是扔掉它。

另外,你的意思是你的文件名是模型/ screenshot.rb,而不是模型/截圖?

+0

是的,'screenshot.rb',謝謝。修正了。當我從'attr_accessible'移除':screenshot'時,出現'Can mass-assign protected attributes:screenshot'錯誤。 –

+0

您正在使用哪個導軌? – ussferox

+0

也可以發表你的表格嗎?你在嵌套嗎? – ussferox

相關問題