重新归档 simple_form 未定义方法 Attachment_field

2024-05-09

I am trying to hook up refile to my view and I am getting the following error: 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 视图(忽略绑定.pry)

= 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未定义。

另外,我有一个初始化程序 refile.rb 设置如下:

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

我想知道文档是否混乱或者 simple_form 部分是否无聊,因为如果我将其切换为:

= jc.input :company_logo, as: :file, direct: true, presigned: true

经过长时间的战斗,我找到了解决这种情况的方法。首先我发现了这个帖子Rails 3 中使用 Bootstrap 设计 simple_form_for 的文件上传按钮样式 https://stackoverflow.com/questions/14509449/styling-file-upload-button-for-simple-form-for-with-bootstrap-in-rails-3这促使我尝试https://github.com/jasny/bootstrap/ https://github.com/jasny/bootstrap/。 Jasny 有一些不错的文件上传器,但所有的精华都已经过时了。 [注意,我希望通过补丁来解决这个问题,因为我这周有时间]所以我将 Jasny 文件下载到供应商 javascripts 和 stylesheets 文件夹中,如下所示:

我也用过https://rails-assets.org/ https://rails-assets.org/在我的 gemfile 中引入holder.js:

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

接下来,我使用以下内容更新了 application.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

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

我想将其重构为部分内容,但我会等到有另一个文件上传表单来解决这个问题。另外,如果您查看上面的代码,我遵循 simple_form 使用的区域设置约定。不漂亮但有效。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

重新归档 simple_form 未定义方法 Attachment_field 的相关文章

  • 仅删除多对多关系

    我有一个 has and belongs to many friends join table gt friends peoples 要添加朋友 我会这样做 people followers lt lt friend这会创建关系和新的个人资
  • 多重要求和允许强参数rails 4

    在下面的情况下 我尝试使用强参数 我想要求email address password并允许remember me fields 但像下面这样使用它只允许最后一行在方法示例中 在下面的情况下 它只需要params permit rememb
  • 我在 Rails 中使用了保留字吗?

    这是我的模型 class Record lt ActiveRecord Base belongs to user belongs to directory end class Directory lt ActiveRecord Base h
  • 即使在急切加载之后,belongs_to 关联也会单独加载

    我有以下关联 class Picture lt ActiveRecord Base belongs to user end class User lt ActiveRecord Base has many pictures end 在我的
  • rvm gem 安装错误?

    我正在摆弄 ruby gems 和 rvm 它工作得很好 但现在当我尝试安装 gem 时出现错误 gem install Rails错误 同时 执行 gem Errno EACCES 权限被拒绝 Users da rvm gems ruby
  • Rails 4 应用程序...在开发环境中,除非刷新页面,否则 javascript 不会触发

    所以我的第一个rails4应用程序遇到了一个奇怪的问题 除非我重新加载页面 否则我的页面javascript不会触发 对于我的 asset pipeline JS 和 content for JS 来说都是如此 在我的 assets jav
  • 使用 rspec 测试嵌套路由

    我正在尝试使用 rspec 测试路由 以下给出了 预期块返回真值 的错误 我不确定我错过了什么 通过浏览器我可以发布到这个网址并且成功 有任何想法吗 谢谢 Routes resources forum topics do resources
  • 如何加载页面特定的rails 4 js文件?

    我正在阅读资产管道的 Rails 指南文档 它指出 CoffeeScript 页面特定生成的文件 如果清单上有 require tree 指令 则默认情况下可供用户使用 这对我不起作用我必须包括这个 在特定控制器上 我缺少什么 资产管道会将
  • 如何向 Rails 应用程序添加自定义字体?

    我想在 RoR 应用程序中使用几种字体 但它们的格式主要是 ttf 和 otf 等 我该如何将这些文件嵌入到我的 Rails 应用程序中 也就是说 一旦我将它们放入我的资产文件夹中 将它们嵌入我的 CSS 和 或 LESS 文件中的语法到底
  • 如何在 Capybara 中 POST 到 URL?

    刚刚从 Cucumber Webrat 切换到 Cucumber Capybara 我想知道如何将内容 POST 到 Capybara 中的 URL 在 Cucumber Webrat 中我能够执行以下步骤 When I send to d
  • ruby on Rails:音频/mp3 内容标题下载

    如何在 ruby rails 中设置下载标题 在 php 中 我为 mp3 下载设置标头 如下所示 header Content Transfer Encoding binary header Content type audio mp3
  • 即使路由存在,Rspec 命名空间路由规范也会失败

    我在用着rspec rails 2 8 1 and 导轨3 1 3 我正在尝试测试 Admin ZonesController 的路由 我已经通过运行验证了浏览器中存在该路由rake routes 我没有使用 ActiveRecord 如果
  • PHP 的脚手架 [关闭]

    Closed 这个问题是无关 help closed questions 目前不接受答案 PHP 中有什么东西可以像 Rails 一样创建基本的脚手架吗 编辑 我需要一些东西来快速原型化 一些框架比如Symfony http www sym
  • Travis-CI 上的 Rails 数据库设置

    我正在尝试在 Rails 项目上使用 Travis 持续集成 文档说 对于 SQLite3 测试数据库必须配置如下 test adapter sqlite3 database memory timeout 500 但我想保留本地测试的默认配
  • Rails 中的字母分页

    我正在寻找 Rails 的宝石用于字母分页 我希望我可以在结果中找到第一个字母的列表 我的意思是 如果没有以 a 开头的行 我不希望 a 显示在分页链接上 这种宝石已经存在了吗 提前致谢 这根本不难创建 例如 如果您有一个find 也许像
  • 我可以使用同一个 ActionMailer 从不同地址发送电子邮件吗

    我正在为我的 Rails 2 3 9 应用程序使用 ActionMailer 当我使用以下方式发送电子邮件时 deliver user invite config def user invite subject content subjec
  • Ruby on Rails 两个同时创建两个不同控制器的操作

    我有一个线程控制器和消息控制器 线程 has many 消息 用户单击发送后 我将数据发送到线程控制器以创建线程 我想做到这一点 线程控制器 rb def create if thread save send data into messa
  • RSpec 无法从 Factorygirl 中找到工厂

    我将在 Rails3 项目中将 RSpec 与 Factory Girl 一起使用 我已经安装了 Factory Girl 但它找不到工厂我有这个错误 Failure Error Factory build user should be v
  • 回形针/乘客 NotIdentifiedByImageMagickError:

    当我尝试上传照片时红宝石 on Rails http en wikipedia org wiki Ruby on Rails using 回形针 http github com thoughtbot paperclip在我的本地机器上它运行
  • Rails 6:每个用户只能创建一个配置文件

    我目前正在开发 Rails 6 应用程序 我有以下关联 用户有一个配置文件 并且配置文件属于用户 当编辑用户的配置文件时 我最终为该用户提供了两个配置文件 我希望每个用户只有一份个人资料 编辑表单 profile edit html erb

随机推荐