简体   繁体   中英

param is missing or the value is empty: user when enabling password access to a page in Rails app

I am getting this error: "param is missing or the value is empty: user" when I am trying to create password restricted access to user settings page in my app. All the logged in users can see a tab to change their two factor settings page. When they are click on the tab for two factor settings, they are required to enter the password for that account. I have a created page for password entry. However I keep getting the above error.

Here is the controller method defined inside two_factor_settings_controller.rb

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

And these are the private methods in the 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 

The view page looks like this:

 .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'

Inside the route file:

  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

Would appreciate any clue to what is causing this error. thanks

According to the documentation of simple_form , here is what your form should look like:

<%= simple_form_for(@user, as: :user, method: :post, url: users_path) do |f| %>
  <%= f.input :name %>
  <%= f.submit 'New user' %>
<% end %>

The most important part in your case is f.submit 'New user' . You're submitting your form outside the form of the gem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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