I'm using Rails 4.1, Ruby 2.0, and ActiveAdmin from github
I'm extremely new to rails. but basically all I want is an active admin form that will allow image uploading, and then save the URL to a string in the model called img_url.
Currently, it doesn't save anything to img_url, just NIL. But the form lets you select a file and proceed.
My activeadmin form:
ActiveAdmin.register Product do
permit_params :name, :price, :description, :is_available,
:img_url
index do
column :img_url, :sortable => false do Product
"<img src='#img_url' alt='Product Logo' style='height:48px; display:block;
margin-left:auto;
margin-right:auto;'".html_safe
end
column :name
column :price do |item|
div :class => "price" do
number_to_currency item.price
end
end
column :description
column :is_available
actions
end
form :html => { :enctype => "multipart/form-data"} do |f|
f.inputs "Details" do
f.input :name
f.input :description, :as => :text
f.input :price
f.input :is_available
f.input :img_url, :label => 'Product Image', :as => :file
end
f.actions
end
end
My product model, which brings me to my second issue:
class Product < ActiveRecord::Base
has_attached_file :img_url, :styles => { :thumb => "100x100" }
validates_attachment :img_url, :content_type => { :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"] }
end
Of course, i'd like to validate the content/type to make sure it's an image. But uncomment the validates method I get:
"undefined method `img_url_content_type' for #<Product:0x000000060e4560>"
Parameters:
{"utf8"=>"✓",
"_method"=>"patch",
"authenticity_token"=>"5dtIAV866xUavdUQDFuL5/J0IIl6Un3l4Nu2sNOmy2c=",
"product"=>{"name"=>"Test",
"description"=>"Test Description",
"price"=>"5",
"is_available"=>"1",
"img_url"=>#<ActionDispatch::Http::UploadedFile:0x000000060ce378 @tempfile=#<Tempfile:/tmp/RackMultipart20141009-8450-2vnymd>,
@original_filename="100x100.gif",
@content_type="image/gif",
@headers="Content-Disposition: form-data; name=\"product[img_url]\"; filename=\"100x100.gif\"\r\nContent-Type: image/gif\r\n">},
"commit"=>"Update Product",
"id"=>"2"
It'd also be nice to be able to delete the file from the server within ActiveAdmin.
Here's my gemfile, too
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.6'
# Use mysql as the database for Active Record
gem 'mysql2'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.3'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
gem 'oj'
gem 'oj_mimic_json'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring', group: :development
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
# Use debugger
# gem 'debugger', group: [:development, :test]
# ActiveAdmin
gem 'activeadmin', github: 'activeadmin'
gem 'devise'
gem 'paperclip', :git => "http://github.com/thoughtbot/paperclip.git"
This is the very first project I've ever done in rails, so if any more information is needed just let me know.
Thanks!
UPDATE
I adjusted the permit_params which was the issue with img_url not saving.
I also had to add a column/migration for img_url_file_name for paperclip.
However, it now requires the validator which I can not get to work still
I'm going to answer my own question, I had a few problems here.
1.) I did not properly define :img_url in permit_params properly.Corrected original question with how it should be
2.) I do not need an img_url column in the database. I made a migration to remove it. My web service will rather use paperclip methods to return the URL when needed
3.) Paperclip needed an img_url_file_name, which I made a migration for.
4.) Paperclip also needed an img_url_content_type in the model, which I also made a migration for.
Now everything works as I expected.