【问题标题】:How can I upload file using carrierwave and nested forms in rails 4?如何在rails 4中使用carrierwave和嵌套表单上传文件?
【发布时间】:2014-05-22 20:49:28
【问题描述】:

您好,我在上传文件时遇到问题。我正在使用carrierwave 上传文件并使用嵌套表单从用户那里捕获文件。当我提交页面时,一切似乎都正常工作,但没有文件上传或保存在数据库中。 https://gist.github.com/jpstokes/10276415

服务器日志

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"=>"john.doe@abc.com",
"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', 'john.doe@abc.com', '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)

【问题讨论】:

  • 可以分享一下你提交嵌套表单时生成的服务器日志吗?
  • 请从ApplicantsController 分享您的permit 相关代码,以便我可以相应地起草我的答案。

标签: ruby-on-rails-4 carrierwave nested-forms


【解决方案1】:

根据服务器日志,有一个警告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

【讨论】:

    猜你喜欢
    • 2016-01-06
    • 1970-01-01
    • 2016-04-05
    • 1970-01-01
    • 1970-01-01
    • 2015-11-21
    • 2013-02-24
    • 2013-11-20
    • 1970-01-01
    相关资源
    最近更新 更多