0

When I'm trying to upload image to database via paperclip, it always ignore the image column. I'm not sure why it doesn't work, I did lots of research about the paperclip, e.g. github, YouTube and did exactly the same as those tutorials. I think I did all of the code correct, but I don't know where the problem comes from, please point that out.

the console information

Started POST "/categories" for 127.0.0.1 at 2015-04-13 17:07:49 +1000
Processing by CategoriesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"DIR1d3lWrcwcylkelLsRm/262BRZqu5w3cmrMP/5LV0=", "category"=>{"name"=>"Food", "parent_category_id"=>"", "description"=>""}, "commit"=>"Create Category"}
(0.1ms)  begin transaction
SQL (0.4ms)  INSERT INTO "categories" ("created_at", "description", "name", "updated_at") VALUES (?, ?, ?, ?)  [["created_at", "2015-04-13 07:07:49.850360"], ["description", ""], ["name", "Food"], ["updated_at", "2015-04-13 07:07:49.850360"]]
(9.3ms)  commit transaction
Redirected to http://localhost:3000/categories/1
Completed 302 Found in 15ms (ActiveRecord: 9.7ms)

Gemfile

source 'https://rubygems.org'


gem 'rails', '4.1.6'
gem 'sqlite3'
gem 'sass-rails', '~> 4.0.3'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'

gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0',          group: :doc

gem 'spring',        group: :development
gem 'paperclip', '~> 4.2.1'
gem 'bootstrap-sass'

gem 'jquery_mobile_rails'

Model class

class Category < ActiveRecord::Base
  has_attached_file :photo, :styles => {:medium => "300x300", :thumb => "100x100"}
  validates_attachment_content_type :photo, :content_type => /\Aimage\/.*\Z/

  has_many :children, class_name: "Category", foreign_key: "parent_category_id"
  belongs_to :parent, class_name: "Category", foreign_key: "parent_category_id"

  #has_many :products
end

And I add the column in controller

def category_params
      params.require(:category).permit(:name, :parent_category_id, :description, :photo)
end

Changed the _form.html.erb file like this

<%= form_for @category, :url => categories_path, :html => {:multipart => true} do |f| %>
    ...
<div class="field">
   <%= f.label :photo %><br>
   <%= f.file_field :photo %>
</div>
    ...

The schema.rb file is like this now:

create_table "categories", force: true do |t|
    t.string   "name"
    t.integer  "parent_category_id"
    t.text     "description"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "photo_file_name"
    t.string   "photo_content_type"
    t.integer  "photo_file_size"
    t.datetime "photo_updated_at"
end
Alex
  • 135
  • 1
  • 15
  • Is there a reason why you're only including the gem in the development group? – abbott567 Apr 13 '15 at 07:54
  • Did I? I think only the gem 'spring' is included in the development group. I changed the position of the paperclip to the top of the Gemfile and the result didn't change. – Alex Apr 13 '15 at 08:26
  • This is not about paperclip. You don't get image attributes in your params. And I don't think you really need `:url => categories_path` there, but it has nothing to do with your problem, just a note. – IvanSelivanov Apr 13 '15 at 09:59
  • I tried different ways about this problem, but can't fix that.. – Alex Apr 13 '15 at 12:35

1 Answers1

0

Problem solved from here, hope this will help someone have the same issue

I think the problem was caused by using jquery_mobile_rails gem, and I need to change the _form.html.erb file like this (add "data-ajax" => false)

= form_for(@category, :html => { :multipart => true, "data-ajax" => false}) do |f|
Community
  • 1
  • 1
Alex
  • 135
  • 1
  • 15