代码之家  ›  专栏  ›  技术社区  ›  Chris Hough

refile simpleform未定义的方法附件字段

  •  4
  • Chris Hough  · 技术社区  · 10 年前

    我试图将refile连接到我的视图,但我得到了以下错误: enter image description here

    以下是我的模型:

    class Job < ActiveRecord::Base
      acts_as_paranoid
    
      acts_as_taggable
      acts_as_taggable_on :languages
    
      belongs_to :company
      belongs_to :category
    
      validates :title,
                :location,
                :category,
                :language_list,
                :short_description,
                :description,
                :application_process,
                presence: true
    end
    
    class Company < ActiveRecord::Base
      acts_as_paranoid
      has_many :jobs
    
      attachment :company_logo, type: :image
    
      validates :name, :url, :email, presence: true
    end
    

    和simple_form视图(忽略binding.priy)

    = render layout: 'header' do
      = simple_form_for(@job) do |j|
        div[class='panel-heading']
          h3[class='panel-title']
            |Position Details
        div[class='panel-body']
          = j.input :title
          = j.input :location
          = j.association :category, as: :radio_buttons
          = j.input :language_list, as: :check_boxes, collection: @tags
          = j.input :short_description
          = j.input :description, input_html: { class: 'textarea-height wysihtml5' }
          = j.input :application_process
        div[class='panel-heading']
          h3[class='panel-title']
            |Company Details
        div[class='panel-body']
          = j.simple_fields_for :company do |jc|
            = jc.input :name
    
            = binding.pry
    
            = jc.input :company_logo, as: :attachment
            = jc.input :url
            = jc.input :email
        div[class='panel-heading panel-heading-info']
          h3[class='panel-title']
            |Would you like your job posting featured?
        div[class='panel-body']
          p
            |Featured posted are promoted with additional design elements to grab the
              job seeker's attention.  This additional marketing capability can be purchased
              for an additonal $#{AppConfig.product['settings']['job_base_featured_price']}.
    
          = j.input :is_featured
    
          = j.button :button, type: 'button', class: 'btn-primary' do
            = job_posting_button_step_label
            =>
            i[class='fa fa-arrow-circle-right']
    

    虽然我一直在分析文档,但我似乎无法理解为什么 attachment_field 未定义。

    此外,我还有一个初始化器重新编译。rb设置如下:

    require 'refile/rails'
    require 'refile/simple_form'
    

    我想知道是文档被弄乱了还是simple_form部分被破坏了,因为如果我将其切换为:

    = jc.input :company_logo, as: :file, direct: true, presigned: true
    
    1 回复  |  直到 10 年前
        1
  •  1
  •   Community Mohan Dere    9 年前

    经过一场漫长的战斗,我找到了解决这种情况的办法。首先我找到了这个SO帖子 Styling file upload button for simple_form_for with Bootstrap in Rails 3 这让我尝试 https://github.com/jasny/bootstrap/ Jasny有一些不错的文件上传器,但所有的宝石都已经过时了。[注意,我希望在本周有时间的时候用补丁来解决这个问题]所以我将Jasny文件下载到了供应商的javascripts和样式表文件夹中,如下所示:

    enter image description here

    我也用过 https://rails-assets.org/ 拉入支架。js在我的gemfile中:

    source 'https://rails-assets.org' do
      gem 'rails-assets-holderjs'
    end
    

    接下来,我更新了我的应用程序。js文件,包含以下内容:

    //= require refile
    //= require holderjs
    //= require jasny-bootstrap
    

    接下来,我需要sass设置中的样式:

    // External Libraries Built For Bootstrap in Vendor
    @import 'awesome-bootstrap-checkbox';
    @import 'jasny-bootstrap';
    

    这为我提供了以简单形式修改文件上传器的工具:

    = j.simple_fields_for :company do |jc|
      = jc.input :name
      div
        label
          = t('simple_form.labels.company.logo')
      div[class="form-group fileinput fileinput-new" data-provides="fileinput"]
        div[class="fileinput-new thumbnail" style="width: 200px; height: 200px;"]
          img[data-src="holder.js/200x200" alt="..."]
        div[class="fileinput-preview fileinput-exists thumbnail" style="max-width: 200px; max-height: 200px;"]
        div
          span[class="btn btn-default btn-file"]
            span[class="fileinput-new"]
              |Select image
            span[class="fileinput-exists"]
              |Change
            = jc.input_field :logo, direct: true, presigned: true
          a[href="#" class="btn btn-default fileinput-exists" data-dismiss="fileinput"]
            |Remove
        p[class="help-block"]
          = t('simple_form.hints.company.logo')      
      = jc.input :url
      = jc.input :email
    

    这导致了一个漂亮的上传器!

    enter image description here

    我想把它重构成一个部分,但我会等到我有另一个文件上传表单来解决这个问题。此外,如果你仔细看一下上面的代码,我遵循了simple_form使用的语言环境约定。虽然不漂亮,但很管用。