【发布时间】:2013-12-27 04:45:40
【问题描述】:
我有一个 Documents 控制器、模型和视图以及一个 DocumentTypes 控制器、模型和视图。一个文档 has_and_belongs_to_many DocumentTypes 和一个 DocumentType has_and_belongs_to_many Documents。我在文档索引模板上有一个表单,在其中添加新文档时,我将 document_type_id 设置为您从下拉列表中选择的任何文档类型。看起来是这样的:
<%= form_for Project.new, :html => { :multipart => true } do |f| %>
<%= select_tag "document[document_type_id][]", options_from_collection_for_select(DocumentType.find(:all), "id", "title") %>
<% end %>
接下来我要做的只是在文档类型显示页面上仅列出分配给当前文档类型的文档。我目前只有这个:
// Controller
def show
...
@documents = Document.find(:all)
end
// View
<% @documents.each do |document| %>
...
<% end %>
我不完全确定在添加新文档时 document_type_id 设置是否正确,所以这很可能是我的问题。我尝试过使用.where,例如:@documents = Document.where(:document_type_id => 1),但在浏览器中查看时只会给我一个空白列表。
我需要在这里做什么?
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 controller associations