嗨我有通过carrierwave上传文件的问题,当我用嵌套对象的形式对它进行ulpading它不起作用 - 我有一个请求,我没有错误,一切正常,但它没有保存存档。我收到了这样的东西:
[0] {
"id" => "",
"name" => "AVATARDALEJNIEDZIALA",
"surname" => "something",
"nick" => "123123",
"image" => "example-midnight-run-artwork.png"
},
它会创建空文件。
/新
<%= render 'multi_form'%>
/ _ multi_form.html.erb
<%= form_tag batch_create_employees_path do %>
<% @employees.each_with_index do |employee, i| %>
<div class="single-employee-fieldset">
<div class="row">
<p class="span12 text-error">
<%= employee.errors.full_messages.join(', ') %>
</p>
</div>
<div class="row">
<%= fields_for "employees[]", employee do |employee_form| %>
<%= employee_form.hidden_field :id %>
<div class="span3">
<%= employee_form.text_field :name %>
</div>
<div class="span3">
<%= employee_form.text_field :surname %>
</div>
<div class="span3">
<%= employee_form.text_field :nick %>
</div>
<div class="span3">
<%= employee_form.file_field :image %>
</div>
<% end %>
</div>
</div>
<% end %>
<div class="kontenter"></div>
<%= submit_tag "Stwórz", :class => 'btn btn-info'%>
<% end %>
这是我控制器中的动作:
attributes = params.permit(employees: [:name, :surname, :nick, :image, :id])[:employees]
attributes = attributes.select { |attrs| attrs.values.any?(&:present?) }
@employees = attributes.map do |attrs|
if attrs[:id].present?
Employee.find(attrs[:id]).update(attrs)
else
p = Employee.create(attrs.merge(active: 1))
end
end
saved, with_errors = @employees.partition { |employee| employee.errors.empty? }
if with_errors.any?
render :new
else
if attributes.present?
flash[:success] = "Zostali zapisani #{saved.map {|x| x.name}.join(', ')}"
end
redirect_to employees_path
end
当我上传没有嵌套表格的时候,Carrierwave会工作,例如https://github.com/carrierwaveuploader/carrierwave
编辑//添加日志
Started GET "/employees" for 127.0.0.1 at 2014-05-27 09:29:32 +0200
Processing by EmployeesController#index as HTML
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = '59dc057f3685ffa672ac20b6c31ebe6145452a06' LIMIT 1
Employee Load (0.4ms) SELECT "employees".* FROM "employees"
Rendered employees/index.html.erb within layouts/application (3.3ms)
Completed 200 OK in 20ms (Views: 14.9ms | ActiveRecord: 0.9ms)
Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/custom.css?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/catalogs.css?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/participants.css?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/employees.css?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/clients.css?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/projects.css?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/sessions.css?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/skills.css?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/users.css?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/vacations.css?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/welcome.css?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/bootstrap-transition.js?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/bootstrap-alert.js?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/bootstrap-affix.js?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/bootstrap-button.js?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/bootstrap-collapse.js?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/bootstrap-dropdown.js?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/bootstrap-carousel.js?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/bootstrap-tooltip.js?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/bootstrap-tab.js?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/bootstrap-scrollspy.js?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/jquery.turbolinks.js?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/bootstrap-modal.js?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/raphael.js?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/bootstrap.js?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/bootstrap-popover.js?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/bootstrap-typeahead.js?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/catalogs.js?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/morris.js?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/employees.js?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/turbolinks.js?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/participants.js?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/projects.js?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/vacations.js?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/sessions.js?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/clients.js?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/users.js?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/skills.js?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/welcome.js?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2014-05-27 09:29:33 +0200
编辑//添加模型:
class Employee < ActiveRecord::Base
has_many :participants, dependent: :destroy
has_many :projects, through: :participants
has_many :vacations
has_and_belongs_to_many :skills
validates :name, presence: true
validates :surname, presence: true
mount_uploader :image, ImageUploader
...
end