Missing parameters in ruby ​on rails

32 Views Asked by At
class AsistenciaController < ApplicationController
  def update
    if @asistencia.update(asistencia_params)
      redirect_to asistencia_path, notice: "Asistencia guardada correctamente"
    else 
      render :edit, status: :unprocessable_entity
    end
  end

  private
    def asistencia
      @asistencia = User.all
    end

    def asistencia_params
      params.require(:asistencia).permit(:email, :username, :password_digest, :entry, :asistencia)
    end
end


class UsersController < ApplicationController
  before_action :set_user, only: %i[ show edit update destroy ]

  def index
    @users = User.all
  end
 
  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)

    respond_to do |format|
      if @user.save
        format.html { redirect_to user_url(@user), notice: "User was successfully created." }
      else
        format.html { render :new, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @user.update(user_params)
        format.html { redirect_to user_url(@user), notice: "User was successfully updated." }
      else
        format.html { render :edit, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @user.destroy

    respond_to do |format|
      format.html { redirect_to users_url, notice: "User was successfully destroyed." }
    end
  end

  private
    def set_user
      @user = User.find(params[:id])
    end

    def user_params
      params.require(:user, :asistencia).permit(:email, :username, :password_digest, :entry, :asistencia)
    end
end

I was hoping that users could be updated through assistance, I added the user parameters to the assistance ones but they failed so I removed them (entry is a boolean)

I have tried changing the parameters but it doesn't work


  Parameters: {"authenticity_token"=>"[FILTERED]", "id"=>"1", "username"=>"Prueba", "entry"=>"false", "commit"=>"Registrar"}
Completed 400 Bad Request in 6ms (Allocations: 714)



ActionController::ParameterMissing (param is missing or the value is empty: asistencia):

app/controllers/asistencia_controller.rb:20:in `asistencia_params'
app/controllers/asistencia_controller.rb:3:in `update'

I need help, I'm new to programming,I don't know what else to change or how to fix it

1

There are 1 best solutions below

0
mechnicov On

Params have [require][1] method

When passed a single key, if it does not exist, raises ActionController::ParameterMissing

In your case when you call @asistencia.update(asistencia_params) it raises error

Your method asistencia_params requires key :asistencia

But your params {"authenticity_token"=>"[FILTERED]", "id"=>"1", "username"=>"Prueba", "entry"=>"false", "commit"=>"Registrar"} don't include it

To fix it you need wrap your updating params in that key