簡體   English   中英

Ruby / Rails ActiveAdmin映像驗證器錯誤

[英]Ruby/Rails ActiveAdmin Image Validator Error

我正在使用來自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

當然,我想驗證內容/類型以確保它是圖像。 但是取消注釋我得到的validates方法:

 "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中完成的項目,因此,如果需要更多信息,請告訴我。

謝謝!

更新

我調整了allow_params,這是img_url無法保存的問題。

我還必須為回形針添加img_url_file_name的列/遷移。

但是,它現在需要驗證器,而我仍然無法使用它

我要回答自己的問題,這里有一些問題。

1.)我沒有在permit_params中正確定義:img_url。更正了原始問題

2.)我不需要數據庫中的img_url列。 我進行了遷移以將其刪除。 我的Web服務寧願在需要時使用回形針方法返回URL

3.)回形針需要一個img_url_file_name,我為此進行了遷移。

4.)回形針在模型中還需要img_url_content_type,我也為此進行了移植。

現在一切正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM