チュートリアルに従ってテストシナリオを作成しました。ユーザーが情報ページの編集に移動したがまだサインインしていない場合、ユーザーはサインインページにリダイレクトされ、サインインした後、編集ページにリダイレクトされます。しかし、チュートリアルのようにすべてを実行したら、テストを実行して失敗しwrong number of arguments (0 for 1)
ます。エラーが発生したためです。
これはエラー情報です:
サインイン後に保護されたページにアクセスしようとした場合の、サインインしていないユーザーに対するAuthenticationPagesの承認により、目的の保護されたページがレンダリングさ
れ ます
。
helpers / sessions_helper.rb:31:in create' #(eval):2:in block(5 level)in ..redirect_back_or'
# ./app/controllers/sessions_controller.rb:11:inclick_button' # ./spec/requests/authentication_pages_spec.rb:58:in
これは私のspec/requests /authentication_pages_spec.rbです
describe "authorization" do
describe "for non-signed-in users" do
let(:user) { FactoryGirl.create(:user) }
describe "when attempting to visit a protected page" do
before do
visit edit_user_path(user)
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
end
describe "after signing in" do
it "should render the desired protected page" do
page.should have_selector('title', text: 'Edit user')
end
end
end
.
.
.
app / helpers / sessions_helper.rbには、2つの方法がstore_location
あり、redirect_back_or
ユーザーがサインインする前にアクセスしたURLアドレスを保存してリダイレクトします。
module SessionsHelper
.
.
.
def redirect_back_or(default)
redirect_to(session[:return_to] || default)
session.delete[:return_to]
end
def store_location
session[:return_to] = request.url
end
end
次にstore_location
、app / controllers/users_controller.rbのメソッドを 使用しました。
class UsersController < ApplicationController
before_filter :signed_in_user, only: [:edit, :update]
before_filter :correct_user, only: [:edit, :update]
.
.
private
def signed_in_user
unless signed_in?
store_location
redirect_to signin_url, notice: "Please sign in."
end
end
そして、私redirect_back_or
はapp / controllers /sessions_controller.rbのメソッドを使用しました:
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by_email(params[:email].downcase)
if user && user.authenticate(params[:password])
sign_in user
redirect_back_or user
else
flash.now[:error] = "Invalid email/password combination"
render 'new'
end
end
このエラーが発生する理由はわかりません。redirect_back_or
メソッドはすでに引数を取っていると思いますuser
が、それでもエラーが発生します。誰かが私がこれを解決するのを手伝ってくれる?本当にありがとう。