【发布时间】: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 我加了他们
-
试试这个。
<%= f.input :project :as => :select, :collection => Project.all, :include_blank => true, :input_html => { :multiple => true } %> -
@Hemali 所以你建议我将“:projects”更改为“:project”?我试过了,我得到了同样的错误。
标签: ruby-on-rails ruby-on-rails-4 formtastic