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

时间:2017-06-26 20:51:10

标签: ruby-on-rails ruby paperclip

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

enter image description here

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

模型/ 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)

1 个答案:

答案 0 :(得分:2)

我看到您的表单需要进行一些更改。

目前看起来像:

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对象相对应。