【问题标题】:Unable to save multiple models with single form using fields_for无法使用 fields_for 保存具有单个表单的多个模型
【发布时间】:2015-10-21 10:46:32
【问题描述】:

我有一个具有 Deal 模型和 Picture 模型的应用程序。我尝试使用 fields_for 标签以单一形式更新这两个模型,但是 交易模型只更新(父模型),而不是图片模型。 我正在使用 carrierwave 上传图片。

Deal.rb

has_many :pictures,:dependent=> :destroy
accepts_nested_attributes_for :pictures, :reject_if => lambda {|a| a[:image].blank?} 
end

图片.rb

belongs_to :deal
mount_uploader :image, ImageUploader

deals_controller.rb

def new
    @deal = Deal.new
    @location = Location.find(params[:loc_id])
    2.times{@deal.pictures.build}
end

def create
    @location =Location.find(params[:loc_id])
    @image
    @deal = @location.deals.build(strong_params)
    if @deal.save
        redirect_to location_path(@location)
    else
        redirect_to root_url
    end
end

private

def strong_params
    params.require(:deal).permit(:deal_name,:category,:deal_type,picture_attributes:[:image])
end

new.html.erb

<h3>create new deal</h3>
<%=form_for @deal,:html=> {multipart: true} do |f|%>
<label>deal name</label>
<%=f.text_field :deal_name%>
<label>category</label>
<%=f.text_field :category%>
<label>type</label>
<%=f.text_field :deal_type%>
<%= hidden_field_tag(:loc_id, @location.id) %>

<%=f.fields_for :pictures do |builder|%>
<%=builder.file_field :image%>
<%end%>

<%=f.submit :submit%>
<%end%>

carrierwave没有问题,我通过在Deal模型中添加一个新的图像列来测试它。它工作正常。

有什么我遗漏的吗。帮助我。提前谢谢。

控制台日志

Parameters: {"utf8"=>"✓", "authenticity_token"=>"gw7iEC+Q4qX0X69yIoqBr/RyX5dAfePqXkBYhGWM1/C/SMugfWbYFoYzm/pQ2Rn3CzMkPtD3Vwqvr4bZjYcgGA==", "deal"=>{"deal_name"=>"stackover", "category"=>"dsfwerdsf", "deal_type"=>"sdfewer", "pictures_attributes"=>{"0"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x007f1892c180c0 @tempfile=#<Tempfile:/tmp/RackMultipart20151021-12944-11d4ykd.png>, @original_filename="Screenshot from 2015-10-16 18:55:40.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"deal[pictures_attributes][0][image]\"; filename=\"Screenshot from 2015-10-16 18:55:40.png\"\r\nContent-Type: image/png\r\n">}, "1"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x007f1892c67e68 @tempfile=#<Tempfile:/tmp/RackMultipart20151021-12944-1liyrcf.png>, @original_filename="Screenshot from 2015-09-28 15:55:20.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"deal[pictures_attributes][1][image]\"; filename=\"Screenshot from 2015-09-28 15:55:20.png\"\r\nContent-Type: image/png\r\n">}}}, "loc_id"=>"2", "commit"=>"submit"}

【问题讨论】:

  • 看起来它应该可以工作 - 你有我们可以看到的任何参数/控制台日志吗?
  • 我猜你需要使用lambda {|a| a['image'].blank?},因为参数可能有字符串而不是符号键。
  • SQL (0.2ms) INSERT INTO "deals" ("deal_name", "category", "deal_type", "location_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["deal_name", "console"], ["category", "dsfer"], ["deal_type", "erwer"], ["location_id", 5], ["created_at", "2015-10-21 10:55:09.335330"], ["updated_at", "2015-10-21 10:55:09.335330"]]
  • 这就是他们在api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/… 上所做的事情。您可以通过 lambda {|a| abort a.inspect } 进行检查
  • 我编辑了问题

标签: ruby-on-rails model carrierwave fields-for


【解决方案1】:

错误是你正在使用:

<%= f.fields_for :pictures do |builder| %>
  <%= builder.file_field :image %>
<% end %>

注意它的pictures 是复数形式。

在您的白名单中,您允许picture_attributes

试试这个:

def strong_params
    params.require(:deal)
          .permit(
             :deal_name,:category,:deal_type,
             pictures_attributes:[:image]
          )
end

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-07
  • 2016-01-04
  • 1970-01-01
  • 1970-01-01
  • 2021-04-26
  • 2020-08-21
  • 1970-01-01
相关资源
最近更新 更多