访问者希望看到所有帖子的列表
git checkout -b f1
rails c # console
rails s # server
spec/features/guest_can_see_all_topics_spec.rb
# coding: utf-8
feature '访问者希望看到所有帖子的列表' do
scenario '访问/topics, 应该显示所有帖子' do
visit '/topics'
page.should have_content "DHH 的公开课"
page.should have_content "Rails3 中 compass 的 IE 使用问题"
page.should have_content "这周二上海搞Ruby Tuesday么?"
end
end
config/route.rb
, 增加topics
的定义resources :topics, only: [:index]
uninitialized constant TopicsController
TopicsController
rails console
中执行[1] pry(main)> generate "controller topics"
create app/controllers/topics_controller.rb
invoke haml
create app/views/topics
=> "Completed"
The action 'index' could not be found for TopicsController
TopicsController
中没有定义index
方法topics_controller.rb
class TopicsController < ApplicationController
def index
end
end
Missing template topics/index, application/index ...
topics/index
模板app/views/topics/index.html.haml
Failure/Error: page.should have_content "DHH 的公开课"
拷贝ui/topics.html.haml
的内容至当前模板
app/views/topics/index.html.haml
= render 'shared/breadcrumb', links: [["Home", ""], ["社区", ""]]
%section#topics
%section#topics_info.box.box-gray.info-box
%span 查看: 默认
%ul.topics.box
- @topics.each do |topic|
%li
%a.span1(href="")
%img(src="#{gravatar_url('[email protected]')}")
%article.span7
%p.topic_title
= link_to topic.title
%p.topic_info
= link_to "分享", nil, class: "node"
%span= " • "
= link_to "knwang", nil, class: "user_link"
%span= " • "
= "published #{time_ago_in_words(topic.created_at)} ago"
%p.replies.span1
%span 12
%section#sidebar
%section#new_topic.box
= link_to "发布新帖", nil, class: "btn btn-success"
%section#stats.box
%ul
%li 社区会员: 4029 人
%li 帖子数: 312 篇
%li 回帖数: 3123 条
undefined methodeach' for nil:NilClass
`TopicsController
的index
方法中没有对@topics
赋值topics_controller.rb
,增加赋值部分class TopicsController < ApplicationController
def index
@topics = Topic.all
end
end
注意:由于是示范项目所以直接使用了 Topic.all,实际项目中绝对不能这样做
uninitialized constant TopicsController::Topic
Topic
这个 model
rails conssole
中执行[2] pry(main)> generate "model topic title"
invoke active_record
create db/migrate/20121229122838_create_topics.rb
create app/models/topic.rb
=> "Completed"
[3] pry(main)>
expected there to be text "DHH 的公开课" in "社区...
spec/features/guest_can_see_all_topics_spec.rb
,增加background
部分# coding: utf-8
feature '访问者希望看到所有帖子的列表' do
background do
Topic.delete_all
Topic.create title: "DHH 的公开课"
Topic.create title: "Rails3 中 compass 的 IE 使用问题"
Topic.create title: "这周二上海搞Ruby Tuesday么?"
end
scenario '访问/topics, 应该显示所有帖子' do
visit '/topics'
Topic.all.each do |topic|
page.should have_content topic.title
end
end
end
git add . git commit git checkout dev git merge f1 --no-ff git push origin dev