[英]param is missing or the value is empty: user when enabling password access to a page in Rails app
當我嘗試在我的應用程序中創建對用戶設置頁面的密碼限制訪問時,我收到此錯誤:“參數丟失或值為空:用戶”。 所有登錄的用戶都可以看到一個選項卡來更改他們的兩個因素設置頁面。 當他們單擊兩個因素設置的選項卡時,他們需要輸入該帳戶的密碼。 我有一個用於輸入密碼的創建頁面。 但是我不斷收到上述錯誤。
這是在 two_factor_settings_controller.rb 中定義的 controller 方法
def authorization_check_for_2fa
@user = current_user
prompt_for_password(current_user)
if authorization_check_params[:password].present?
debugger
if current_user.valid_password?(authorization_check_params[:password])
flash.now[:success] = "Yay correct password"
redirect_to two_factor_settings_path
else
flash.now[:alert] = "Sorry, wrong password"
end
end
end
這些是 controller 中的私有方法:
def prompt_for_password(user)
@user = user
session[:otp_user_id] = user.id
render authorization_check_for_2fa_two_factor_settings_path
end
def authorization_check_params
params.require(:user).permit(:password)
end
視圖頁面如下所示:
.card.mb-3
.card-header
%h5.mb-0 Restricted access
.card-body
= simple_form_for @user, url: authorization_check_for_2fa_two_factor_settings_path, html: { method: :post } do |f|
.form-inputs
= f.input :password, input_html: { autocomplete: "current-password" }, required: true
.form-actions
= submit_button 'Submit', class: 'btn btn-primary'
在路由文件中:
resource :two_factor_settings do
get :authorization_check_for_2fa, on: :member
post :authorization_check_for_2fa, on: :member
get :regenerate_backup_codes, on: :member
end
將不勝感激導致此錯誤的任何線索。 謝謝
根據simple_form的文檔,您的表單應如下所示:
<%= simple_form_for(@user, as: :user, method: :post, url: users_path) do |f| %>
<%= f.input :name %>
<%= f.submit 'New user' %>
<% end %>
在您的案例中,最重要的部分是f.submit 'New user'
。 您在 gem 表單之外提交表單。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.