目录:http://ruby-china.org/topics/7770 上一步:使用 RSpec+Capybara 简单 BDD 入门 -9
用户希望看到最新回复的信息
帖子列表页面上显示最新回复的信息
补充:
git checkout -b f10
rails c # console
rails s # server
spec/features/topics_spec.rb
,增加最新回复的验证部分scenario '应该显示帖子的最新回复的信息' do
page.should have_content "last replied by #{@user.name} less than a minute ago"
end
expected there to be text "last replied by test_user less than a minute ago" i...
topics/_topics.html.haml
拷贝ui/topics.html
中最新回复的部分至当前模板
%span= " • "
last replied by
= link_to "knwang", nil, class: "user_link"
4 mintes ago
修改为
- last_replay = topic.replies.last
- if last_replay
%span= " • "
last replied by
= link_to last_replay.user.name, last_replay.user, class: "user_link"
= "#{time_ago_in_words(last_replay.created_at)} ago"
在测试用例的background
部分加入replay
的数据
Topic.last.replies.create content: 'test replay', user: @user
Can't mass-assign protected attributes: user
user
的访问属性models/reply.rb
,增加用户关联并设置访问属性class Reply < ActiveRecord::Base
attr_accessible :content, :topic, :user
belongs_to :topic
belongs_to :user
end
rails console
中执行[32] pry(main)> generate "migration AddUserIdToReplies user_id:integer"
invoke active_record
create db/migrate/20130103120615_add_user_id_to_replies.rb
=> "Completed"
[33] pry(main)>
bundle exec rake db:migrate
bundle exec rake db:test:prepare
git add .
git commit
git checkout dev
git merge f10 --no-ff
git branch -d f10