anon
anon

Reputation:

Ruby On Rails 5.0.5 CarrierWave Gem works on Update not on Create

I'm having a bug with CarrierWave gem with this setup:

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

I'm using a form_for to try and upload an image like:

f.file_field :image

and in the create method I only get back the image name which doesn't save, forcing an exception on create, I can see:

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

while on update the image saves fine and when I force an exception I get something like:

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

is there any reason that the #<ActionDispatch::Http::UploadedFile:... is not working for me on create?

/**************************************************************************************************/ EDIT 1: multipart => true didn't change anything Here is part of the controller thats relevantenter 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

As for error messages there aren't any! The model simply uploads without saving the image. logs for Create:

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",

Logs for Update (successful):

"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"}

I am starting to think it may be something with the Uploader store_dir method as the image files appear to save in the public folder under the model id and because a model has yet to be created could it possibly be that carrier wave finds an empty id and just doesn't save?

As for the form, its super boring!

<%= 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" %>

I really appreciate your help, guys!

Upvotes: 0

Views: 86

Answers (2)

anon
anon

Reputation:

I ended up solving this by removing the model that I had created and recreating the model using the Scaffold Generator and then everything ran correctly

Upvotes: 1

Roshan
Roshan

Reputation: 915

You might be missing multipart: true in your form_for.

form_for @user, html: { multipart: true } 

Upvotes: 0

Related Questions