目录:http://ruby-china.org/topics/7770 上一步:使用 RSpec+Capybara 简单 BDD 入门 -10
用户希望对帖子进行回复
git checkout -b f11
rails c # console
rails s # server
spec/features/guest_can_see_topic_info_spec.rb
,增加回复 form 的验证部分scenario '应该显示回复用的form' do
visit "/topics/#{@topic.id}"
page.should have_field 'reply_content'
page.should have_button '提交回复'
end
expected to find field "replay_content" but there were no matches
topics/show.html.haml
拷贝ui/topic.html
中回复 form 的部分至当前模板
%section#reply.box
%form(action="" method="post")
%textarea(rows="4")
%input(type="submit" class="btn btn-primary" value="提交回复")
修改为
%section#reply.box
= form_for [@topic, Reply.new] do |f|
= f.text_area :content, rows: 4
= f.submit "提交回复", class: "btn btn-primary"
undefined methodtopic_replies_path' for #<#<Class:0x007fc0f8
`config/routes.rb
,增加嵌套的 replay 的路由设置resources :topics, only: [:index, :show, :new, :create] do
resources :replies, only: [:create]
end
scenario '输入回复内容点击"提交回复"后,正常提交回复' do
visit "/topics/#{@topic.id}"
fill_in 'reply_content', with: "回复测试"
click_button "提交回复"
current_path.should == topic_path(@topic)
page.should have_content "回复测试"
end
uninitialized constant RepliesController
replies
这个controller
rails console
中执行[34] pry(main)> generate "controller replies"
create app/controllers/replies_controller.rb
invoke haml
create app/views/replies
=> "Completed"
[35] pry(main)>
The action 'create' could not be found for RepliesController
create
方法controllers/replies_controller.rb
,增加create
方法def create
end
Missing template replies/create, application/create with {:local...
replies/create
模板controllers/replies_controller.rb
,增加实际逻辑class RepliesController < ApplicationController
def create
topic = Topic.find(params[:topic_id])
reply = topic.replies.build params[:reply]
reply.user = current_user
reply.save
redirect_to topic
end
end
git add .
git commit
git checkout dev
git merge f11 --no-ff
git branch -d f11
!! 全部完成!!