我很新的軌道,我試圖顯示用戶在我的應用程序中所做的所有帖子。我沒有在這個論壇上通過類似的帖子,但沒有成功。我做了一些名稱,標題和內容屬性的腳手架,然後創建了一個名爲Blogger的模型,其中包含'name'屬性。顯示RAILS中的單個用戶的所有帖子
這裏是我的代碼
控制器
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」 錯誤,說:
未定義的方法`後」的零:NilClass,並在提取的源index.htm.erb代碼
什麼是你'Blogger'類?很重要的,因爲你嘗試從這個模型接近帖子:> –
一爲博客名爲「名稱」的字符串。每個博客可以創建多個帖子。我希望列出博客作者點擊其名稱時發佈的所有帖子。感謝您的幫助 – user1063028
您的整個班級「Blogger」是一個名爲'name'的單個字符串?不確定這是否正確,因爲您參考了'blogger.name'和'blogger.posts'。 –