【问题标题】:Ruby On Rails 5.0.5 CarrierWave Gem works on Update not on CreateRuby On Rails 5.0.5 CarrierWave Gem 适用于更新而不是创建
【发布时间】:2017-11-19 17:46:26
【问题描述】:

我在使用此设置时遇到了 CarrierWave gem 的错误:

gem 'carrierwave',             '1.1.0'
gem 'mini_magick',             '4.7.0'
gem 'fog',                     '1.40.0'

我正在使用form_for 尝试上传如下图片:

f.file_field :image

create 方法中,我只取回不保存的图像名称,强制创建异常,我可以看到:

{ object => {"image" => "image_name.jpg"} }

在更新图像时保存良好,当我强制异常时,我得到类似的东西:

{ object => {"image" => #<ActionDispatch::Http::UploadedFile:0x007f9d251c10a0...} }

#&lt;ActionDispatch::Http::UploadedFile:... 在创建时是否对我不起作用?

/**************************************************** ****************************************************** **/ 编辑 1: multipart => true 没有改变任何东西 这是相关的控制器的一部分enter code here

class BuildingsController < ApplicationController 
...
def new
  @user = current_user
  @building = Building.new
  @regions = Region.active 
end

def create
  @user = current_user
  @building = Building.new(building_params)
  if @building.save
    flash[:success] = "Building Saved!"
    redirect_to buildings_path
  else
    @regions = Region.where(active: true)
    render 'new'
  end
end

def edit
  @user = current_user
  @building = Building.find(params[:id])
  @regions = Region.where(active: true)
end

def update
  @building = Building.find(params[:id])
  if @building.update_attributes(building_params)
    flash[:success] = "Building updated"
    redirect_to buildings_url
  else
    @regions = Region.where(active: true)
    render 'edit'
  end
end

def destroy
    Building.find(params[:id]).destroy
  flash[:success] = "Building deleted"
  redirect_to buildings_url
end



def building_params
  params.require(:building).permit(:code, :name, :region_id, :address,
                                 :postal_code, :units, :city, :floors,
                                 :year_build, :image, :roof_anchors,
                                 :roof_type, :roof_replaced, :roof_quantity,
                                 :windows_type, :windows_doors,
                                 :windows_replaced,:windows_panes,:upg_type, 
                                 :upg_levels,        :upg_replaced,         
                                 :walls_type,        :walls_replaced)
  end
...
end

至于错误信息,没有任何!模型只是上传而不保存图像。 创建日志:

Started POST "/buildings" for ::1 at 2017-08-25 11:01:26 -0400
Processing by BuildingsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"sRVGLqtWsOVNGAN2wiGNkraLXQ9lCFHExP4vYhUb2q60nI0ScfNkitTsforCS9hH+0/UF2GoRfzlZyNhGodM0g==", "building"=>{"code"=>"new code", "name"=>"new", "region_id"=>"4", "address"=>"123 new street", "city"=>"Ottawa", "postal_code"=>"K1l8j9", "units"=>"10", "floors"=>"10", "year_build"=>"1978", "image"=>"Screen Shot 2014-11-14 at 1.18.18 PM.png", "roof_anchors"=>"", "roof_type"=>"", "roof_replaced"=>"", "roof_quantity"=>"", "windows_type"=>"", "windows_doors"=>"", "windows_replaced"=>"", "windows_panes"=>"", "upg_type"=>"", "upg_levels"=>"", "upg_replaced"=>"", "walls_type"=>"", "walls_replaced"=>""}, "commit"=>"Submit"}
logs for update: 
{"utf8"=>"✓",
 "_method"=>"patch",

更新日志(成功):

"authenticity_token"=>"E8PYsQ2qw0ftl30UZsojTlfHO4jD7F4y0//tMowTsOkWShON1w8XKHRjAOhmoHabGgOykMdMSgryZuExg48mlQ==",
 "building"=>
  {"code"=>"NEW CODE",
   "name"=>"New",
   "region_id"=>"4",
   "address"=>"123 New Street",
   "city"=>"Ottawa",
   "postal_code"=>"K1L8J9",
   "units"=>"10",
   "floors"=>"10",
   "year_build"=>"1978",
   "image"=>
    #<ActionDispatch::Http::UploadedFile:0x007f848ece4978
     @content_type="image/jpeg",
     @headers="Content-Disposition: form-data; name=\"building[image]\"; filename=\"parlament.jpg\"\r\nContent-Type: image/jpeg\r\n",
     @original_filename="parlament.jpg",
     @tempfile=#<File:/var/folders/nx/yt012_z17_qgl7y410p8dgk80000gn/T/RackMultipart20170825-31617-lovcfi.jpg>>,
   "roof_anchors"=>"",
   "roof_type"=>"",
   "roof_replaced"=>"",
   "roof_quantity"=>"",
   "windows_type"=>"",
   "windows_doors"=>"",
   "windows_replaced"=>"",
   "windows_panes"=>"",
   "upg_type"=>"",
   "upg_levels"=>"",
   "upg_replaced"=>"",
   "walls_type"=>"",
   "walls_replaced"=>""},
 "commit"=>"Submit",
 "id"=>"226"}

我开始认为这可能与 Uploader store_dir 方法有关,因为图像文件似乎保存在模型 ID 下的公共文件夹中,并且由于尚未创建模型,因此可能是载波找到了id 为空,就是不保存?

至于形式,超级无聊!

<%= form_for(@building) do |f| %>
<%= render 'shared/error_messages', object: @building %>

<h3>General</h3>

<%= f.label :code, class: 'required' %>
<%= f.text_field :code, class: 'form-control' %>

<%= f.label :name, class: 'required'  %>
<%= f.text_field :name, class: 'form-control' %>

<% if is_regional_admin?(@user) %>
      <%= f.label :region_id, class: 'required'  %>
      <%= f.collection_select(:region_id, @regions.where(id: @user.region.id), :id, :name) %>
<% else %>
      <%= f.label :region_id, class: 'required'  %>
      <%= f.collection_select(:region_id, @regions, :id, :name) %>
<% end %>

<%= f.label :address, class: 'required'  %>
<%= f.text_field :address, class: 'form-control' %>

<%= f.label :city, class: 'required'  %>
<%= f.text_field :city, class: 'form-control' %>

<%= f.label :postal_code, class: 'required'  %>
<%= f.text_field :postal_code , class: 'form-control' %>

<%= f.label :units, class: 'required'  %>
<%= f.text_field :units, class: 'form-control' %>

<%= f.label :floors, class: 'required'  %>
<%= f.text_field :floors, class: 'form-control' %>

<%= f.label :year_build, class: 'required'  %>
<%= f.text_field :year_build, class: 'form-control' %>

<%= f.label :image %>
<%= f.file_field :image, html: { multipart: true } , class: 'form-control' %>

<h3>Roof</h3>

<%= f.label :roof_anchors %>
<%= f.text_field :roof_anchors, class: 'form-control' %>

<%= f.label :roof_type %>
<%= f.text_field :roof_type, class: 'form-control' %>

<%= f.label :roof_replaced %>
<%= f.text_field :roof_replaced, class: 'form-control' %>

<%= f.label :roof_quantity %>
<%= f.text_field :roof_quantity, class: 'form-control' %>

<h3>Windows</h3>

<%= f.label :windows_type %>
<%= f.text_field :windows_type, class: 'form-control' %>

<%= f.label :windows_doors %>
<%= f.text_field :windows_doors, class: 'form-control' %>

<%= f.label :windows_replaced %>
<%= f.text_field :windows_replaced, class: 'form-control' %>

<%= f.label :windows_panes %>
<%= f.text_field :windows_panes, class: 'form-control' %>

<h3>Updagres</h3>

<%= f.label :upg_type %>
<%= f.text_field :upg_type, class: 'form-control' %>

<%= f.label :upg_levels %>
<%= f.text_field :upg_levels, class: 'form-control' %>

<%= f.label :upg_replaced %>
<%= f.text_field :upg_replaced, class: 'form-control' %>

<h3>Walls</h3>

<%= f.label :walls_type %>
<%= f.text_field :walls_type, class: 'form-control' %>

<%= f.label :walls_replaced %>
<%= f.text_field :walls_replaced, class: 'form-control' %>


<%= f.submit "Submit", class: "btn btn-primary" %>

非常感谢你们的帮助,伙计们!

【问题讨论】:

  • <:http::uploadedfile:0x007f9d251c10a0>
  • 如果下面的答案不起作用,请粘贴您的完整错误消息和控制器
  • 发布create的表单代码

标签: ruby-on-rails carrierwave form-for


【解决方案1】:

我最终通过删除我创建的模型并使用脚手架生成器重新创建模型来解决这个问题,然后一切都运行正常

【讨论】:

    【解决方案2】:

    您的form_for 中可能缺少multipart: true

    form_for @user, html: { multipart: true } 
    

    【讨论】:

    • 添加multiple: true如何解决问题?
    • @Pavan 没有可以参考的错误日志,所以我最好的猜测是form_for 中缺少multiple: true
    • multiple: true 仅在您想为该字段发送多个值时使用。我猜你的意思是multipart :true
    • @Pavan 啊!我不知道我怎么忽略了这一点。感谢您指出,我已经更新了答案。
    • 虽然multipart: true 只适用于表单,而不适用于字段。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多