目录:http://ruby-china.org/topics/7770 上一步:使用 RSpec+Capybara 简单 BDD 入门 -3
访问者希望看到帖子的详细信息
git checkout -b f4
rails c # console
rails s # server
spec/features/guest_can_see_topic_info_spec.rb
# coding: utf-8
feature '访问者希望看到帖子的详细信息' do
background do
@node = Node.create name: "Ruby"
@topic = Topic.create title: "topic 1 test", node: @node
end
scenario '访问/topics/1, 应该显示帖子的详细信息' do
visit "/topics/#{@topic.id}"
page.should have_content @topic.title
page.should have_content @topic.node.name
end
end
No route matches [GET] "/topics/1"
topics/1
对应的路由信息config/routes.rb
,增加topics show
的路由设置resources :topics, only: [:index, :show]
The action 'show' could not be found for TopicsController
TopicsController
没有定义show
方法controllers/topics_controller.rb
,增加show
方法定义def show
end
Missing template topics/show, application/show with {:lo...
show
模板topics/show.html.haml
,拷贝ui/topic.html.haml
的内容至当前模板expected there to be text "topic 1 test" in "社区 会...
topics/show.html.haml
内容= render 'shared/breadcrumb', links: [["Home", ""], ["社区", ""], ["新手问题", ""], ["浏览帖子", ""]]
%section#show_topic
%section#topic_banner.box.info-box
%h1= @topic.title
%p.topic_info
= link_to @topic.node.name, @topic.node, class: "node"
%span= " • "
= link_to "knwang", nil, class: "user_link"
%span= " • "
= "published #{@topic.created_at} ago"
%section#topic_content.box
%p= @topic.content
%section#sidebar
%section#new_topic.box
= link_to "发布新帖", nil, class: "btn btn-success"
%section#stats.box
%ul
%li 社区会员: 4029 人
%li 帖子数: 312 篇
%li 回帖数: 3123 条
注意:回复的部分在后面的步骤中做,暂时删掉即可
undefined methodtitle' for nil:NilClass
`@topic
变量赋值controllers/topics_controller.rb
def show
@topic = Topic.find(params[:id])
end
undefined methodcontent' for #<Topic:0x0...
`topic
的时候忘了content
属性了 :)rails console
中执行[5] pry(main)> generate "migration AddContentToTopics content:text"
invoke active_record
create db/migrate/20130101134732_add_content_to_topics.rb
=> "Completed"
[6] 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
end
scenario '访问/topics/1, 应该显示帖子的详细信息' do
visit "/topics/#{@topic.id}"
page.should have_content @topic.title
page.should have_content @topic.content
page.should have_content @topic.node.name
page.should have_content "published #{@topic.created_at} ago"
end
end
Can't mass-assign protected attributes: content
content
的访问属性model/topic.rb
attr_accessible :title, :node, :content
page.should have_link "Home", href: root_path
page.should have_link @topic.node.name, href: node_path(@topic.node)
page.should have_link "浏览帖子", href: topic_path(@topic)
undefined local variable or methodroot_path' for...
`nroot_path
的定义config/routes.rb
,增加root_path
的定义MyRubyChina::Application.routes.draw do
resources :topics, only: [:index, :show]
resources :nodes, only: [:show]
root to: "topics#index"
match 'ui/:action', controller: 'ui'
...
rm public/index.html
expected to find link "Home" but there were no matches. Als...
topics/show.html.haml
,修改顶部导航栏链接= render 'shared/breadcrumb', links: [["Home", root_path], [@topic.node.name, @topic.node], ["浏览帖子", @topic]]
git add .
git rm public/index.html
git commit
git checkout dev
git merge f4 --no-ff
git branch -d f4