0

所以我开始在管理后端工作,并试图显示所有应用程序供管理员查看。我有一个接受嵌套属性的应用程序模型,但我的管理控制器似乎无法访问它们。这是代码,我出人意料地卡住了。我收到的错误是:

undefined method 'postsecondaries' for #<ActiveRecord::Relation::ActiveRecord_Relation_App:0x00000102452128>

管理员控制器

class Admin::AppsController < ApplicationController

  def index
    @apps = App.all
    @users = User.all
  end


end

索引视图:

<h4>Post Secondary Schools of Interest</h4>
<% @apps.postsecondaries.each do |f| %>
Post Secondary Name: <%= f.postsecondary %><br />
Address: <%= f.postsecondary_address %><br />
City: <%= f.postsecondary_city %><br />
Province: <%= f.postsecondary_province %><br />
Postal Code: <%= f.postsecondary_postalcode %><br />
Country: <%= f.postsecondary_country %><br />
Program: <%= f.postsecondary_program %><br />
Faculty: <%= f.postsecondary_faculty %><br />
Status: <%= f.postsecondary_status %><br />
<% end %> 

路线:

  namespace :admin do
    resources :apps
  end 

还有公共应用模型:

class App < ActiveRecord::Base
  belongs_to :user
  has_many :postsecondaries, :dependent => :destroy


  accepts_nested_attributes_for :postsecondaries, :allow_destroy => true

end

架构:

create_table "postsecondaries", force: true do |t|
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "app_id"
    t.string   "postsecondary"
    t.string   "postsecondary_address"
    t.string   "postsecondary_city"
    t.string   "postsecondary_province"
    t.string   "postsecondary_postalcode"
    t.string   "postsecondary_country"
    t.string   "postsecondary_program"
    t.string   "postsecondary_faculty"
    t.string   "postsecondary_status"
  end
4

3 回答 3

0

@apps是s 的一个RelationApp而不是一个App。因此它没有关联方法postsecondaries

据推测,您想遍历其中@apps的每个App,遍历postsecondaries. 虽然您可以按照@zeantsoi 的答案所示执行此操作,但Rails 有一个基于部分的简写,它允许您通过在index视图中包含以下内容来避免显式的顶级迭代:

<% render @apps %>

然后,您的app部分将只包含用于迭代和显示Postsecondary元素的代码(或对部分使用相同的技术postsecondary)。有关详细信息,请参阅http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials

最后请注意,由于您似乎没有提供有关app记录其余部分的任何信息,而只是提供有关 的信息,因此postsecondaries您可能希望首先收集所有Postsecondary值并消除重复项和/或在显示它们之前对其进行排序。

于 2013-09-11T20:28:57.540 回答
0

基本上,您将不得不遍历每个应用程序,然后遍历 postsecondary 以显示信息......所以尝试这样的事情

<h4>Post Secondary Schools of Interest</h4>
<% @apps.each do |app| %>
   <% app.postsecondaries.each do |ps| %>
      Post Secondary Name: <%= ps.postsecondary %><br />
      Address: <%= ps.postsecondary_address %><br />
      City: <%= ps.postsecondary_city %><br />
      Province: <%= ps.postsecondary_province %><br />
      Postal Code: <%= ps.postsecondary_postalcode %><br />
      Country: <%= ps.postsecondary_country %><br />
      Program: <%= ps.postsecondary_program %><br />
      Faculty: <%= ps.postsecondary_faculty %><br />
      Status: <%= ps.postsecondary_status %><br />
   <% end %> 
<% end %>
于 2013-09-11T20:46:58.287 回答
0

现在,您正在尝试访问ActiveRecord::Relation. 相反,您应该遍历关系对象并单独访问对象的属性。

尝试在您的关联中更深入地遍历一个级别:

<% @apps.each do |app| %>
  <% app.postsecondaries.each do |ps| %>
    Post Secondary Name: <%= ps.postsecondary %><br />    
    <!-- continue iterating through each attribute on `ps` -->
  <% end %>
<% end %> 
于 2013-09-11T20:49:49.227 回答