Ruby on rails 4 带有载波和嵌套形式的Rails-如何保存嵌套属性

Ruby on rails 4 带有载波和嵌套形式的Rails-如何保存嵌套属性,ruby-on-rails-4,carrierwave,nested-forms,nested-attributes,Ruby On Rails 4,Carrierwave,Nested Forms,Nested Attributes,我有一个模型用户,它嵌入照片(与carrier\u wave\u direct一起使用)。在photo.rb模型中,我添加了一个字段描述。 这是photo.rb(载波直接): 下面是user.rb: class User ... accepts_nested_attributes_for :photos, allow_destroy: true, :reject_if => proc {|attributes| attributes['file'].blank?} ... e

我有一个模型用户,它嵌入照片(与carrier\u wave\u direct一起使用)。在photo.rb模型中,我添加了一个字段描述。 这是photo.rb(载波直接):

下面是user.rb:

class User
   ...
  accepts_nested_attributes_for :photos, allow_destroy: true, :reject_if => proc {|attributes| attributes['file'].blank?}
  ...
end
以下是用户_controller.rb:

def create
  @user = users.build(user_params)
  if @user.save
    @user.photos.each do |photo|
      unless photo.file.file == nil
        photo.save!
      end  
    end
  else
    render :new
  end
end

def update
  if @user.update_attributes(user_params)
  @user.photos.each do |photo|
    unless photo.file.file == nil
      photo.save!
    end  
  end
...
end

def user_params
  params.require(:user).permit(
    :photos_attributes => [:id, :description, :file, :_destroy]
  )
end
我可以在控制台中看到启动参数的字段描述与表单的正确输入,但我无法保存它。这张照片保存得很好。 表格:

<%= f.fields_for :photos do |photo_f| %>
  <%= render partial: 'photo_fields', locals: { f: photo_f } %>
  <%= link_to_add_association raw('<i class="fi-plus">&nbsp; add a photo</i>'), f, :photos %>
<% end %>

在部分嵌套字段中:

<div class="nested-fields">
  <% if f.object.file.to_s.empty? %>
    <%= f.file_field :file, label: "Upload a photo." %>
  <% else %>
    <%= image_tag f.object.file %>
  <% end %>
  <%= f.input :description, label: false, as: :text %>
</div>


我真的不知道如何将description字段保存到模型中,它是在params中传递的,我在控制台中没有看到任何错误消息。在显示视图中,如果我对用户执行.inspect操作,则说明字段为零(因为它无法保存)

我确信这是模型中的
属性['file'].空白?
,因为当您使用
图像标签f.object.file
在页面上显示图像时,如果有图像,请尝试更改
属性['file'].blank?
带有
(attributes['file'].\124; attributes['description'])。blank?
并希望您在各自的模型中进行空验证它正在工作!!!非常感谢。我是否需要对每个字段进行这种检查,即使该字段不是强制性的?
<div class="nested-fields">
  <% if f.object.file.to_s.empty? %>
    <%= f.file_field :file, label: "Upload a photo." %>
  <% else %>
    <%= image_tag f.object.file %>
  <% end %>
  <%= f.input :description, label: false, as: :text %>
</div>