看来我需要重温一下我在 Rails 中的联想。目前我正在尝试显示所有以部门名称为员工的帖子。
目前有两种模式,岗位和部门
class Post < ActiveRecord::Base
belongs_to :department
attr_accessible :title, :comments, :department_id
end
class Department < ActiveRecord::Base
has_many :posts
attr_accessible :name, :post_id
#Scopes
scope :staff_posts, where(:name => "Staff")
end
所以我想显示所有具有部门名称人员的帖子
为此,我已将其放入我的控制器中
class PublicPagesController < ApplicationController
def staffnews
@staffpost = Department.staff_posts
end
end
在我看来,我正试图像这样显示所有这些帖子
<% @staffpost.each do |t| %>
<h2><%= t.title %>
<h2><%= t.comments %></h2>
<% end %>
当我得到未定义的方法 nil 时,显然某处出错了,即使我有 3 个名为“员工”的帖子
有人可以解释一下我在哪里误解了协会,因为我很想把这个做对
编辑
路线
scope :controller => :public_pages do
get "our_news"
match "our_news/staffnews" => "public_pages#staffnews"