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