在邁克爾·哈特爾的Ruby on Rails的教程,第二版,第9章的演習6,說:第9章練習6 Michael Hartl的Ruby on Rails教程:什麼是解決方案?
Signed-in users have no reason to access the new and create actions in the Users controller. Arrange for such users to be redirected to the root URL if they do try to hit those pages.
怎樣才能寫出這個RSpec的測試?我想這
describe "POST on Users#create" do
before { post users_path }
specify { response.should redirect_to(root_path) }
end
我已經採用的do /結束塊嘗試,將用戶的屬性的散列等上述溶液中加入在the official sample code行#162的代碼片段。所有這些給我這個錯誤:
Failures:
1) Authentication authorization as non-admin user POST on Users#create
Failure/Error: before { post users_path }
AbstractController::DoubleRenderError:
Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return".
# ./app/controllers/users_controller.rb:27:in `create'
# ./spec/requests/authentication_pages_spec.rb:72:in `block (5 levels) in <top (required)>'
Finished in 2.72 seconds
88 examples, 1 failure
至於實際的目標是訪問限制到新創建的用戶控制器動作,我加入這行解決了他們:
admin_user if signed_in?
我知道這是有效的,因爲我手工測試了它。唯一的問題是我無法爲它寫一個rspec測試。 The code I have created as I follow this tutorial is available at github.
爲什麼我得到這個錯誤?我究竟做錯了什麼?解決辦法是什麼?謝謝。
你的錯誤描述說'渲染和/或重定向在此action'被稱爲多次使用的before_filter。你可以編輯你的問題,在你的'UsersController'中添加'create'方法的內容。 –
你好保羅,謝謝你的迴應。我已根據要求編輯了我的帖子。但爲了讓你更容易,我的代碼是[在這個鏈接](https://github.com/haloflightleader/sample_app)。其中,你應該能夠看到我的用戶控制器的全部榮耀。再次感謝你的幫助。 – user1976515