繁体   English   中英

参数丢失或值为空:在 Rails 应用程序中启用对页面的密码访问时的用户

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM