0

我正在使用来自 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 中从服务器中删除文件也很好。

这也是我的gemfile

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 添加一列/迁移。

但是,它现在需要我仍然无法工作的验证器

4

1 回答 1

0

我要回答我自己的问题,我在这里遇到了一些问题。

1.)我没有在 permit_params 中正确定义 :img_url 。更正了原始问题应该如何

2.) 我不需要数据库中的 img_url 列。我进行了迁移以将其删除。我的 Web 服务将在需要时使用回形针方法返回 URL

3.) Paperclip 需要一个 img_url_file_name,我为其进行了迁移。

4.) 回形针还需要模型中的 img_url_content_type,我也对其进行了迁移。

现在一切都按我的预期工作。

于 2014-10-09T23:14:22.570 回答