因此,這是使用泊塢窗,它包括我從頭開始建立一些用戶身份驗證一個非常基本的Rails應用程序5(不使用設計等)。現在,我想開始學習關於水豚的請求規格,但是我正在碰到一個看起來很奇怪的問題。要求在水豚一個GET,當它應該是一個POST
這是我的登錄表單(sessions.new.erb
):
<%= form_tag sessions_path do %>
<form class="m-t" role="form" action="/">
<div class="form-group">
<%= text_field_tag :email, params[:email], class: 'form-control', placeholder: "Email Address", required: "" %>
</div>
<div class="form-group">
<%= password_field_tag(:password, nil, class: 'form-control', placeholder: "Password", required: "") %>
</div>
<div class="form-group">
<%= check_box_tag :remember_me, 1, params[:remember_me] %>
<%= label_tag :remember_me %>
</div>
<div class="actions"><%= submit_tag "Log In", class: "btn btn-primary block full-width m-b" %></div>
</form>
<% end %>
而且我requests/sessions_spec.rb
:
require "rails_helper"
RSpec.feature "Login", :type => :feature do
scenario "handles wrong email and password gracefully" do
visit login_path
fill_in "Email Address", :with => "something"
fill_in "Password", :with => "something"
click_button "Log In"
expect(page).to have_text("Email or password incorrect")
end
end
現在,這個,如果你手工測試,所以我會假設水豚會看到同樣的事情的作品。但它一直在失敗。我有配置的應用程序,這樣,如果你試圖訪問一個受保護控制器和您沒有登錄,它會將您重定向到/login
和閃爍的消息說Please log in to see this page
。 Rspec測試返回,這是奇怪的 - 這表明水豚試圖訪問另一頁。
所以我尾的測試日誌(docker-compose run web tail -f log/test.log
)
而且我發現我百思不得其解:
Started GET "/login" for 127.0.0.1 at 2017-10-17 06:59:26 +0000
Processing by SessionsController#new as HTML
Rendering sessions/new.html.erb within layouts/empty
Rendered sessions/new.html.erb within layouts/empty (1.1ms)
Completed 200 OK in 6ms (Views: 6.1ms | ActiveRecord: 0.0ms)
Started GET "/?email=something&password=[FILTERED]&commit=Log+In" for 127.0.0.1 at 2017-10-17 06:59:26 +0000
Started GET "/locations" for 127.0.0.1 at 2017-10-17 06:59:26 +0000
Processing by LocationsController#index as HTML
Redirected to http://www.example.com/login
Filter chain halted as :authenticate rendered or redirected
Completed 302 Found in 2ms (ActiveRecord: 0.0ms)
Started GET "/login" for 127.0.0.1 at 2017-10-17 06:59:26 +0000
Processing by SessionsController#new as HTML
Rendering sessions/new.html.erb within layouts/empty
Rendered sessions/new.html.erb within layouts/empty (1.1ms)
Completed 200 OK in 6ms (Views: 4.9ms | ActiveRecord: 0.0ms)
(0.4ms) ROLLBACK
第一位是好的,弄登錄由SessionsController#新的處理。但隨後,(參見第6行)由於某種原因,水豚試圖讓根URL,通過電子郵件/密碼PARAMS英寸我的根URL映射到LocationsController#指數,這對用戶是不允許訪問,因此得到重定向回/登錄消息Please log in to see this page
。那個按鈕實際上做的是發送一個POST到SessionsController#create。如果你看日誌,當你做手工,這正是發生了什麼:
web_1 | Started POST "/sessions" for 172.18.0.1 at 2017-10-17 07:02:19+0000
web_1 | Processing by SessionsController#create as HTML
我不能,爲什麼在水豚當你按下按鈕,它執行完全不同的請求時,單擊該按鈕摸出手動。
幫助非常感謝!
呃,完全錯過了嵌套的形式。很明顯,現在你指出了它!非常感謝,也提到了你關於功能和請求規格的觀點。 –