2014-04-09 26 views
0

嘿,我有一個上傳文件的問題。我正在使用carrierwave上傳文件和嵌套表單以捕獲用戶的文件。當我提交頁面時,所有內容都可以正常工作,但不會上傳文件,也不會保存在數據庫中。 https://gist.github.com/jpstokes/10276415如何在rails 4中使用carrierwave和嵌套窗體上載文件?

Server日誌

Started POST "/applicants" for 127.0.0.1 at 2014-04-09 10:20:12 -0500 
Processing by ApplicantsController#create as HTML 
Parameters: {"utf8"=>"✓", "authenticity_token"=>"N+hSir8O8GjW3Q6TEgO4i8Yg3id37G/MhJgPuIHbjhQ=", 
"applicant"=>{"fname"=>"Jason", "lname"=>"Brown", "phone"=>"555-555-5555", 
"alt_phone"=>"555-555-5555", "email"=>"[email protected]", 
"address"=>"123 ABC Lane",  "city"=>"Wonderland", "state"=>"KS", "zip"=>"12345", 
"country"=>"United States", "education"=>"High School", "salary"=>"100000", "resumes_attributes"=> 
{"0"=>{"name"=>"My resume", "attachment"=>#<ActionDispatch::Http::UploadedFile:0x007fbcceba6cc8 
@tempfile=#<Tempfile:/var/folders/4s/z8bwrnjn20b13_w1tw7lnskw0000gn/T/RackMultipart20140409-11899- 
11xnpch>, @original_filename="Document1.docx", @content_type="application/vnd.openxmlformats- 
officedocument.wordprocessingml.document", @headers="Content-Disposition: form-data;  
name=\"applicant[resumes_attributes][0][attachment]\"; filename=\"Document1.docx\"\r\nContent-Type: 
application/vnd.openxmlformats-officedocument.wordprocessingml.document\r\n">}}, "job_id"=>"1"}, 
"skills"=>[{"name"=>"PHP", "last_used"=>"04-12-2012", "years_experience"=>"3"}, {"name"=>"", 
"last_used"=>"", "years_experience"=>""}], "commit"=>"Submit"} 
Unpermitted parameters: resumes_attributes, job_id 
(0.1ms) BEGIN 
SQL (0.2ms) INSERT INTO `applicants` (`address`, `alt_phone`, `city`, `country`, `created_at`, 
`education`, `email`, `fname`, `lname`, `phone`, `salary`, `state`, `updated_at`, `zip`) VALUES 
('123 ABC Lane', '555-555-5555', 'Wonderland', 'United States', '2014-04-09 15:20:12', 'High 
School', '[email protected]', 'Jason', 'Brown', '555-555-5555', 100000, 'KS', '2014-04-09 15:20:12',  
12345) 
(0.1ms) COMMIT 
Skill Load (0.2ms) SELECT `skills`.* FROM `skills` WHERE `skills`.`name` = 'PHP' LIMIT 1 
(0.1ms) BEGIN 
SQL (0.1ms) INSERT INTO `skill_sets` (`applicant_id`, `created_at`, `last_used`, `skill_id`, 
`updated_at`, `years_experience`) VALUES (294, '2014-04-09 15:20:12', '2012-12-04', 12, '2014-04-09 
15:20:12', 3) 
(16.4ms) COMMIT 
Skill Load (0.3ms) SELECT `skills`.* FROM `skills` WHERE `skills`.`name` = '' LIMIT 1 
(0.1ms) BEGIN 
SQL (0.2ms) INSERT INTO `skill_sets` (`applicant_id`, `created_at`, `skill_id`, `updated_at`) 
VALUES (294, '2014-04-09 15:20:12', 15, '2014-04-09 15:20:12') 
(0.6ms) COMMIT 
Job Load (0.2ms) SELECT `jobs`.* FROM `jobs` WHERE `jobs`.`id` = 1 LIMIT 1 
(0.1ms) BEGIN 
SQL (0.2ms) INSERT INTO `job_applications` (`applicant_id`, `job_id`) VALUES (294, 1) 
(0.5ms) COMMIT 
Redirected to http://localhost:3000/jobs/1 
Completed 302 Found in 29ms (ActiveRecord: 19.2ms) 
+0

你能分享,當你提交與上傳文件嵌套形式生成的服務器日誌? –

+0

請分享'ApplicantsController'中的'permit'相關代碼,以便我可以相應地起草我的答案。 –

回答

0

按照服務器日誌,有一個警告Unpermitted parameters: resumes_attributes, job_id

您需要在此處執行的操作是在ApplicantsController中允許resumes_attributes和job_id。

例如:

def applicant_params 
    params.require(:applicant).permit(:fname, :lname, :phone,..., resumes_attributes: [:name, :attachment,...]) 
    end 
+0

Thx 100萬!!! –

+0

很高興幫助:) –

相關問題