Search code examples
ruby-on-railsruby-on-rails-4carrierwave

Rails 4: why isn't Carrierwave uploading/writing/saving images?


Carrierwave isn't saving or writing or uploading the images, even with the out-of-the box basic tutorial single-image example. All the (basic) bits of code are in the right places, and it doesn't throw up any error messages on the front end, appearing to work, saving the new 'newsitem', but without the selected image.

The console throws up:

Started PATCH "/newsitems/63" for ::1 at 2015-08-08 22:24:43 +0200
Processing by NewsitemsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"gbbna69pcZY1h8fG5s/4CdbFw48D0FakaWehZURUSFcfPPJcuamyduFLtazYvjlRrhXgurEirxceUSxoK4RQwg==", "newsitem"=>{"slug"=>"asdfasf", "source"=>"", "url"=>"", "item"=>"asdfasdf", "story_ids"=>["3", ""], "region_ids"=>["2", ""], "category_ids"=>["5", ""], "main"=>"24m-podemos-ballot-papers.jpg"}, "commit"=>"Update Newsitem", "id"=>"63"}
Newsitem Load (0.1ms)  SELECT

blah, blah, blah…

Crucially within that, to save you from scrolling all the way to the right, it picks up:

"main"=>"24m-podemos-ballot-papers.jpg"

But then further down the PATCH statement, we get to:

SQL (0.4ms)  UPDATE "newsitems" SET "main" = ?, "updated_at" = ? WHERE "newsitems"."id" = ?  [["main", nil], ["updated_at", "2015-08-08 20:24:43.490668"], ["id", 63]]

The 'new', 'edit' and 'show' pages don't throw up any errors. 'New' and 'edit' appear to let me add the image, it saves the newsitem, and then shows it on its new page, but no image at all. So somewhere between the form field and the database, something isn't working.

I checked to make sure ':main' is in the newsitems controller parameters:

def newsitem_params
  params.require(:newsitem).permit(:item, :main, :source, :slug, :url, :region_ids => [], :category_ids => [], :story_ids => [])
end

Any ideas?


Solution

  • Ok, fixed it. Alfie's comment got me searching for things related to updating attributes, and I found a page where someone mentioned the params were being entered badly because of the form set up. So I went back to look at that, and lo and behold…

    On the 'newsitems' form, I had originally tried to put the Carrierwave form elements and the html multipart stuff within the existing editing form, a nested form if you like, which was what was throwing the update method off, like this:

    <%= form_for(@newsitem) do |f| %>
      rails error notice stuff
    <%= form_for @newsitem, :html => {:multipart => true} do |f| %>
      old form + carrierwave stuff
    <% end %>
    <% end %>
    

    Whereas the answer is just:

    <%= form_for @newsitem, :html => {:multipart => true} do |f| %>
      rails error notice stuff
      old form + carrierwave stuff
    <% end %>
    

    So thanks Alfie, every little comment counts.