2013-01-14 43 views
1

在邁克爾·哈特爾的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.

爲什麼我得到這個錯誤?我究竟做錯了什麼?解決辦法是什麼?謝謝。

+0

你的錯誤描述說'渲染和/或重定向在此action'被稱爲多次使用的before_filter。你可以編輯你的問題,在你的'UsersController'中添加'create'方法的內容。 –

+0

你好保羅,謝謝你的迴應。我已根據要求編輯了我的帖子。但爲了讓你更容易,我的代碼是[在這個鏈接](https://github.com/haloflightleader/sample_app)。其中,你應該能夠看到我的用戶控制器的全部榮耀。再次感謝你的幫助。 – user1976515

回答

1

保羅,謝謝你的線索。我解決了這個問題。

我試圖通過添加

admin_user if signed_in? 

解決客觀雖然這出現在瀏覽器中運行,還有些別的東西在後臺繼續。更仔細地尋找到我的創建行動之後,這是我作出的變化和RSpec的測試開始工作:

diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb 
index 6e0fec8..53f8325 100644 
--- a/app/controllers/users_controller.rb 
+++ b/app/controllers/users_controller.rb 
@@ -8,8 +8,8 @@ class UsersController < ApplicationController 
    end 

    def new 
- admin_user if signed_in? 
- @user = User.new 
+ signed_in? ? admin_user : @user = User.new 
+ #@user = User.new 
    end 

    def show 
@@ -17,14 +17,17 @@ class UsersController < ApplicationController 
    end 

    def create 
- admin_user if signed_in? 
- @user = User.create(params[:user]) 
- if @user.save 
-  sign_in @user 
-  flash[:success] = "Welcome to the Sample App!" 
-  redirect_to @user 
- else 
-  render 'new' 
+ if signed_in? 
+  admin_user 
+  else 
+  @user = User.create(params[:user]) 
+  if @user.save 
+  sign_in @user 
+  flash[:success] = "Welcome to the Sample App!" 
+  redirect_to @user 
+  else 
+  render 'new' 
+  end 
    end 
    end 

的解決方案是將代碼封裝成完整的if/else塊。如果我不這樣做,那看起來代碼會繼續存在,就像創建操作一樣。這並不是新操作的問題,因爲在簡單三元之後,它只分配了一個實例變量。

總而言之,我只需要保羅的一條非常好的線索,以及一個良好的睡眠。謝謝。

+0

這很棒,你能解決你自己的問題!請將此標記爲接受其他人蔘考的答案:-) –

2

我只是通過教程去我自己和我在這個確切的練習,所以我肯定不是專家。不過,我正在以不同的方式解釋請求。據我瞭解,如果任何登錄用戶嘗試用戶新建或創建操作,而不僅僅是非管理員,則請求將重定向到根頁面。

在任何情況下,我見過的最完美的解決方案是在https://stackoverflow.com/a/11287798/1008891

相關問題