控制器代码
def show
@topic = Topic.find(params[:id])
@reply = @topic.replies
respond_to do |format|
format.html # show.html.erb
format.json { render json: @topic }
end
end
模板代码
<div class="reply">
<% if [email protected]? %>
<%= @reply.each do |reply| %>
<span><%= reply.text %><br /></span>
<% end %>
<% end %>
</div>
网页显示效果(抱歉,不会截图)不应该出现下面类似代码的文字
2.很好 ,很好。
2.很好 ,很好。
2.很好 ,很好。
2.很好 ,很好。
[#<Reply id: 6, user_id: 2, topic_id: 2, text: "2.很好 ,很好。", created_at: "2012-08-30 09:16:08", updated_at: "2012-08-30 09:16:08">, #<Reply id: 7, user_id: 2, topic_id: 2, text: "2.很好 ,很好。", created_at: "2012-08-30 09:16:08", updated_at: "2012-08-30 09:16:08">, #<Reply id: 8, user_id: 2, topic_id: 2, text: "2.很好 ,很好。", created_at: "2012-08-30 09:16:08", updated_at: "2012-08-30 09:16:08">, #<Reply id: 9, user_id: 2, topic_id: 2, text: "2.很好 ,很好。", created_at: "2012-08-30 09:16:08", updated_at: "2012-08-30 09:16:08">]
在 rails c 命令行中显示是没问题的
1.9.3p194 :036 > u.topics.find(1).replies
Topic Load (0.2ms) SELECT "topics".* FROM "topics" WHERE "topics"."user_id" = 1 AND "topics"."id" = ? LIMIT 1 [["id", 1]]
Reply Load (0.4ms) SELECT "replies".* FROM "replies" WHERE "replies"."topic_id" = 1
=> [#<Reply id: 1, user_id: 1, topic_id: 1, text: "1.很好 ,很好。", created_at: "2012-08-30 09:16:07", updated_at: "2012-08-30 09:16:07">, #<Reply id: 2, user_id: 1, topic_id: 1, text: "1.很好 ,很好。", created_at: "2012-08-30 09:16:07", updated_at: "2012-08-30 09:16:07">, #<Reply id: 3, user_id: 1, topic_id: 1, text: "1.很好 ,很好。", created_at: "2012-08-30 09:16:08", updated_at: "2012-08-30 09:16:08">, #<Reply id: 4, user_id: 1, topic_id: 1, text: "1.很好 ,很好。", created_at: "2012-08-30 09:16:08", updated_at: "2012-08-30 09:16:08">, #<Reply id: 5, user_id: 1, topic_id: 1, text: "1.很好 ,很好。", created_at: "2012-08-30 09:16:08", updated_at: "2012-08-30 09:16:08">]
1.9.3p194 :037 >
哪里出的问题真的找不到答案 seeds.rb 中 我是这样添加的代码
Reply.delete_all
5.times do |t|
Reply.create(:user_id => '1', :topic_id => '1', :text => '1.很好 ,很好。')
end
4.times do |t|
Reply.create(:user_id => '2', :topic_id => '2', :text => '2.很好 ,很好。')
end