使用 Paperclip Gem in Ruby on Rails 将所有输入字段转换为按钮

Converting all input fields to button using Paperclip Gem in Ruby on Rails

我正在处理我的第一个 Rails 项目并安装了 Paperclip Gem 来处理图像上传。照原样,它工作正常,但所有上传字段都显示为单独填写,如下面的屏幕截图所示。

下面是我的代码,所有图像输入字段都是单独分解的。

models/project.rb

class Project < ApplicationRecord
  has_many :tasks

  validates :name, presence: true, length: { maximum: 50 }
  validates :content, presence: true, length: { maximum: 500 }
  validates :price, presence: true, numericality: { only_integer: true }

  has_attached_file :avatar, styles: { medium: '680x300>', thumb: '170x75>' }, default_url: '/images/:style/missing.png"'
  validates_attachment_content_type :avatar, content_type: '/\Aimage\/.*\z/'
end

admin/project.rb

ActiveAdmin.register Project do
  permit_params :name, :content, :price, :image

  show do |t|
    attributes_table do
      row :name
      row :content
      row :price
      row :image do
        project.image? ? image_tag(project.image.url, height: '100') : content_tag(:span, 'No Photo Yet')
      end
    end
  end

  # form html: { enctype: 'multipart/form-data' } do |f|
  #   f.input do
  #     f.input :name
  #     f.input :content
  #     f.input :price
  #     f.input :image, hint: f.project.image? ? image_tag(project.image.url, height: '100') : content_tag(:span, 'Upload JPG/PNG/GIF Image')
  #   end
  #   f.actions
  # end
end

我知道有一种方法可以获取所有这些输入并自动将它们作为上传按钮的一部分,但是我很难使用代码;当我取消注释 admin/project.rb 代码的另一部分时,我不断收到以下错误。

我正在使用 ActiveAdmin、Paperclip 5.1 和 Rails 5.1.1

Started GET "/admin/projects/2/edit" for 127.0.0.1 at 2017-06-26 13:14:33 -0700
Processing by Admin::ProjectsController#edit as HTML
  Parameters: {"id"=>"2"}
  AdminUser Load (0.1ms)  SELECT  "admin_users".* FROM "admin_users" WHERE "admin_users"."id" = ? ORDER BY "admin_users"."id" ASC LIMIT ?  [["id", 2], ["LIMIT", 1]]
  Project Load (0.1ms)  SELECT  "projects".* FROM "projects" WHERE "projects"."id" = ? LIMIT ?  [["id", 2], ["LIMIT", 1]]
  Rendering /Users/rooster/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activeadmin-1.0.0/app/views/active_admin/resource/edit.html.arb
  CACHE Project Load (0.0ms)  SELECT  "projects".* FROM "projects" WHERE "projects"."id" = ? LIMIT ?  [["id", 2], ["LIMIT", 1]]
  Rendered /Users/rooster/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activeadmin-1.0.0/app/views/active_admin/resource/edit.html.arb (129.0ms)
Completed 500 Internal Server Error in 163ms (ActiveRecord: 1.4ms)



ActionView::Template::Error (wrong number of arguments (given 0, expected 1..2)):
    1: insert_tag renderer_for(:edit)

我看到必须对您的表单进行一些更改。

目前看起来像:

form html: { enctype: 'multipart/form-data' } do |f|
  f.input do
    f.input :name
    f.input :content
    f.input :price
    f.input :image, hint: f.project.image? ? image_tag(project.image.url, height: '100') : content_tag(:span, 'Upload JPG/PNG/GIF Image')
  end
  f.actions
end

它应该看起来像:

form html: { enctype: 'multipart/form-data' } do |f|
  f.inputs do
    f.input :name
    f.input :content
    f.input :price
    f.input :image, hint: resource.project.image? ? image_tag(project.image.url, height: '100') : content_tag(:span, 'Upload JPG/PNG/GIF Image')
  end
  f.actions
end

进行了两项小改动。将 input 更改为 inputs,这是在查看 ActiveAdmin 关于 forms 的文档后得到的。接下来我将 f.project.image? 更改为 resource.project.image?resource 对应于您的 Project 对象。