【问题标题】:Formtastic unknown input error - Unable to find input class for selectFormtastic 未知输入错误 - 无法找到选择的输入类
【发布时间】:2014-11-09 08:11:25
【问题描述】:

在我的应用程序中,我有一个 Gallery 类和一个 Project 类,它们与 has_and_belongs_to_many 关系相连。我正在尝试添加将项目添加到画廊的功能,使用下拉选择将数据库中的所有当前项目填充为选项。我创建新画廊的表格如下:

<%= semantic_form_for @gallery do |f| %>
  <%= f.inputs do %>
    <%= f.input :title %>
    <%= f.input :projects, :as => :select, :collection => Project.all,
                :include_blank => true, :input_html => { :multiple => true } %>
  <% end %>

  <%= f.actions do %>
    <%= f.action :submit, :as => :input %>
  <% end %>
<% end %>

但是,当我转到新的图库页面时,我收到以下错误

Formtastic::UnknownInputError in Galleries#new
Showing /app/views/galleries/_form.html.erb where line #4 raised:
Unable to find input class for select

我尝试遵循in the documentation 定义的规范,为什么会出现此错误?我需要在我的 gallery_controller.rb 中添加一些内容吗?

编辑 - 这是两个模型

class Gallery < ActiveRecord::Base
  has_and_belongs_to_many :projects
  belongs_to :user
  validates :title, uniqueness: true
  validates :title, length: { minimum: 4 }
  validates :user, presence: true
end

class Project < ActiveRecord::Base
  belongs_to :user
  has_many :comments
  has_and_belongs_to_many :galleries

  validates :title, presence: true
  validates :thumbnail, presence: true

  def root_comments
    comments.where parent_id: nil
  end
end

【问题讨论】:

  • 你能在这里添加两个模型吗
  • @Hemali 我加了他们
  • 试试这个。 &lt;%= f.input :project :as =&gt; :select, :collection =&gt; Project.all, :include_blank =&gt; true, :input_html =&gt; { :multiple =&gt; true } %&gt;
  • @Hemali 所以你建议我将“:projects”更改为“:project”?我试过了,我得到了同样的错误。

标签: ruby-on-rails ruby-on-rails-4 formtastic


【解决方案1】:

事实证明,这是 Formtastic 和 Formtastic-bootstrap 之间的 gem 版本的问题。根据this issue

formtastic-bootstrap 3.0 实际上只支持 formtastic 2.x。

我使用的是 Formtastic 3.x 版,因此降级到 Formtastic 2.x 解决了我的问题。这就是我更新 Gemfile 的方式:

gem 'formtastic', '~> 2.0'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多