【发布时间】:2011-11-24 03:07:59
【问题描述】:
我对 Rails 很陌生,我正在尝试在我的应用中显示用户发布的所有帖子。我确实在这个论坛上浏览过类似的帖子,但没有成功。我用 name 、 title 和 content 属性做了一些脚手架,然后创建了一个名为 Blogger 的模型,带有 'name' 属性。
这是我的代码
控制器
class BloggersController < ApplicationController
def show
@blogger = blogger.find(params[:id])
@title = @blogger.name
@posts = @blogger.posts
respond_with(@posts)
end
end
查看
Index.html.erb
<% @blogger.post.each do |post| %>
post.name
# Comments to the Post
post.comments.each do |comment|
comment.comments
<% end %>
型号
class Post < ActiveRecord::Base
validates :name, :presence=>true
validates :title, :presence=>true
has_many :comments
belongs_to :blogger
belongs_to :topic, :touch => true
accepts_nested_attributes_for :blogger
attr_accessible :name, :title, :content
end
我收到“NoMethodError in Posts#index”错误提示
nil:NilClass 的未定义方法 `post' 并在提取的源代码中包含 index.htm.erb 的代码
【问题讨论】:
-
Blogger课堂上有什么?非常重要,因为您尝试使用此模型处理帖子:> -
博主的名为“name”的字符串。每个博主都可以创建多个帖子。当我点击一个博主的名字时,我希望列出所有的帖子。感谢您的帮助
-
您的整个班级
Blogger是一个名为name的字符串?不确定这是否完全正确,因为您引用了blogger.name和blogger.posts。
标签: ruby-on-rails