目录:http://ruby-china.org/topics/7770 上一步:使用 RSpec+Capybara 简单 BDD 入门 -7
用户希望看到帖子的回复列表
git checkout -b f9
rails c # console
rails s # server
spec/features/guest_can_see_topic_info_spec.rb
,增加回复部分的验证scenario '应该显示帖子的回复信息' do
visit "/topics/#{@topic.id}"
page.should have_content "共收到 5 条回复"
end
expected there to be text "共收到 5 条回复" in "社区 会员...
topics/show.html.haml
拷贝ui/topic.html.haml
的回复部分到当前模板
%section#topic_content.box
%p= @topic.content
%section#replies_banner.box.info-box
%span 共收到 5 条回复
%section#replies.box
%ul
%li
%a.span1(href="")
%img(src="#{gravatar_url('[email protected]')}")
%article.span8
= link_to "knwang", nil, class: "user_link"
%span two hours ago
%p ++ -- 这个操作就是罪恶的源泉。。++ -- 这个操作就是罪恶的源泉。。++ -- 这个操作就是罪恶的源泉。。++ -- 这个操作就是罪恶的源泉。。++ -- 这个操作就是罪恶的源泉。。++ -- 这个操作就是罪恶的源泉。。
%li
%a.span1(href="")
%img(src="#{gravatar_url('[email protected]')}")
%article.span8
= link_to "knwang", nil, class: "user_link"
%span two hours ago
%p ++ -- 这个操作就是罪恶的源泉。。
%li
%a.span1(href="")
%img(src="#{gravatar_url('[email protected]')}")
%article.span8
= link_to "knwang", nil, class: "user_link"
%span two hours ago
%p ++ -- 这个操作就是罪恶的源泉。。
%section#sidebar
%section#new_topic.box
并修改为
%section#topic_content.box
%p= @topic.content
%section#replies_banner.box.info-box
%span= "共收到 #{@topic.replies.count} 条回复"
%section#replies.box
%ul
- @topic.replies.each do |reply|
%li
%a.span1(href="")
%img(src="#{gravatar_url('[email protected]')}")
%article.span8
= link_to "knwang", nil, class: "user_link"
%span= "#{time_ago_in_words(reply.created_at)} ago"
%p= reply.content
%section#sidebar
%section#new_topic.box
注意:回复的user
暂时先不考虑
undefined methodreplies' for #Topic:0x007fc0f5c4b3b0...
`replies
定义models/topic.rb
,增加关联定义has_many :replies
uninitialized constant Topic::Reply
Reply
这个model
的定义rails console
中执行[26] pry(main)> generate "model reply content:text topic_id:integer"
invoke active_record
create db/migrate/20130103064335_create_replies.rb
create app/models/reply.rb
=> "Completed"
[27] pry(main)>
bundle exec rake db:migrate
bundle exec rake db:test:prepare
# coding: utf-8
feature '访问者希望看到帖子的详细信息' do
background do
@node = Node.create name: "Ruby"
@topic = Topic.create title: "topic 1 test", content: "topic 1 content", node: @node
5.times.map.with_index { |i| Reply.create content: "reply #{i}", topic: @topic, user: user }
end
scenario '应该显示帖子的详细信息' do
......
end
scenario '应该显示帖子的回复信息' do
visit "/topics/#{@topic.id}"
page.should have_content "共收到 #{@topic.replies.count} 条回复"
@topic.replies.each do |reply|
page.should have_content reply.content
end
end
end
Can't mass-assign protected attributes: topic, user
topic,user
的访问属性models/reply.rb
,增加访问属性并设置 topic 和 user 的关联class Reply < ActiveRecord::Base
attr_accessible :content, :topic
belongs_to :topic
end
git add .
git commit
git checkout dev
git merge f9 --no-ff
git branch -d f9