我被卡住了,無法弄清楚我錯了。我也發現了類似的話題,但沒有解決這些錯誤:卡在ROR教程CH 9 Ex。 9
1)用戶頁面刪除鏈接作爲管理員用戶管理員訪問索引頁
Failure/Error: it { should have_link('delete', href: user_path(User.first)) }
expected link "delete" to return something
# ./spec/requests/user_pages_spec.rb:127:in `block (5 levels) in <top (required)>'
2)用戶頁面刪除鏈接作爲管理員用戶管理員訪問索引頁面應該能夠刪除其他用戶
Failure/Error: expect { click_link('delete') }.to change(User, :count).by(-1)
Capybara::ElementNotFound:
no link with title, id or text 'delete' found
# (eval):2:in `click_link'
# ./spec/requests/user_pages_spec.rb:129:in `block (6 levels) in <top (required)>'
# ./spec/requests/user_pages_spec.rb:129:in `block (5 levels) in <top (required)>'
測試代碼
describe "delete links" do
it { should_not have_link('delete') }
describe "as an admin user" do
let(:admin) { FactoryGirl.create(:admin) }
before do
sign_in admin
visit users_path
end
it { should have_link('delete', href: user_path(User.first)) }
it "should be able to delete another user" do
expect { click_link('delete') }.to change(User, :count).by(-1)
end
it { should_not have_link('delete', href: user_path(admin)) }
end
end
控制器:
def
User.find(params[:id]).destroy
flash[:success] = "User destroyed."
redirect_to users_url
end
指數:
<% provide(:title, 'All users') %>
<h1>All users</h1>
<%= will_paginate %>
<ul class="users">
<%= render @users %>
</ul>
<%= will_paginate %>
部分
<li>
<%= gravatar_for user, size: 52 %>
<%= link_to user.name, user %>
<% if current_user.admin? && !current_user?(user) %>
| <%= link_to "delete", user, method: :delete,
data: { confirm: "You sure?" } %>
<% end %>
</li>
任何指導/幫助將不勝感激....
叫什麼名字和局部的路徑?在'visit users_path'後面顯示'puts page.body'是什麼? – prusswan
你可以添加一個'save_and_open_page'來確認,但我敢打賭你沒有任何'@用戶'來渲染;所以沒有刪除鏈接顯示。確認? –
prusswan,感謝您的反饋,部分是_user.html.erb並位於views/users文件夾中。我不確定你在哪裏看到放置page.body – sethb78