我正在使用来自github的Rails 4.1,Ruby 2.0和ActiveAdmin
我对rails非常陌生。但基本上我想要的只是一个允许图像上传的活动管理表单,然后将URL保存到名为img_url的模型中的字符串。
目前,它不会将任何内容保存到img_url,只是NIL。但是表单允许您选择文件并继续。
我的activeadmin表单:
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
我的产品型号,它将我带到了第二个问题:
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
当然,我想验证内容/类型以确保它是一张图片。但取消注释我得到的验证方法:
"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"
能够在ActiveAdmin中从服务器删除文件也很好。
这也是我的宝石文件
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"
这是我在rails中做过的第一个项目,所以如果需要更多信息,请告诉我。
谢谢!
更新
我调整了permit_params,这是img_url没有保存的问题。
我还必须为回形针添加img_url_file_name的列/迁移。
然而,它现在需要验证器,我无法继续工作
答案 0 :(得分:0)
我要回答我自己的问题,我在这里遇到了一些问题。
1。)我没有在permit_params中正确定义:img_url。修正原始问题应该如何
2.)我在数据库中不需要img_url列。我进行了迁移以删除它。我的Web服务宁愿使用回形针方法在需要时返回URL
3.。)Paperclip需要一个img_url_file_name,我进行了迁移。
4.。)Paperclip还需要在模型中使用img_url_content_type,我也进行了迁移。
现在一切都按照我的预期运作。