代码之家  ›  专栏  ›  技术社区  ›  Charles Smith

在Ruby on Rails中使用回形针Gem将所有输入字段转换为按钮

  •  1
  • Charles Smith  · 技术社区  · 7 年前

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

    enter image description here

    模型/项目.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
    

    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、回形针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 回复  |  直到 7 年前
        1
  •  2
  •   jdgray    7 年前

    我看到你的表格需要做一些修改。

    目前看起来像:

    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