管理视图中的NoMethodError

时间:2013-09-11 20:24:38

标签: ruby-on-rails

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

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

3 个答案:

答案 0 :(得分:0)

@appsRelation的{​​{1}},而不是App。因此它没有关联方法App

据推测,您希望遍历postsecondaries并对其中的每个@apps进行迭代,迭代App。虽然您可以像@zeantsoi的答案中所示那样做,但Rails有一个基于部分的简写,通过在postsecondaries视图中包含以下内容,您可以避免显式顶级迭代:

index

您的<% render @apps %> 部分将只包含代码以迭代并显示app元素(或使用与Postsecondary部分相同的技术)。有关详细信息,请参阅http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials

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

答案 1 :(得分: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 %>

答案 2 :(得分: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 %>