Rails 网站显示不正常,找不出哪里有问题

tiseheaini · 2012年08月30日 · 最后由 tiseheaini 回复于 2012年08月31日 · 2311 次阅读

控制器代码

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

<%= @reply.each do |reply| %> 去掉这个等号 <% @reply.each do |reply| %>

#1 楼 @fahchen 粗心大意真要人命呀!!我的肺都快炸了,问题解决了

另外 reply 应该用 replies 才合理。

#3 楼 @huacnlee 是指变量吗??还是别的

#4 楼 @tiseheaini 变量

@replies = @topic.replies
需要 登录 后方可回复, 如果你还没有账号请 注册新账号