Params missing ruby

90 Views Asked by At

Asistencia controller gets everything from user is CRUD where user, entry, password, username exists

These are the parameters that should be updated and change the entry

class AsistenciaController < UsersController
  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(:id, :username, :entry)
  end

end

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

This is el EBR the asistencia

<h1>Marcar Asistencia</h1>

<%= form_with model: @asistencia, url: '/guardar_asistencia', method: :patch do |form| %> 
    <%= form.label :id, style: "display: block" %>
    <%= form.text_field :id %>
    <br>
    <%= form.label :username, style: "display: block" %>
    <%= form.text_field :username %>
    <br>
    <%= form.label :entry, "Entrada o salida:" %>
    <%= form.select :entry, [["Entrada", true], ["Salida", false]] %>
    <%= form.submit 'Registrar' %>
<%end%>

I tried changing the parameters, and everything remains the same, I ran out of ideas to fix it I am relatively new to ruby ​​and need help

Hash of params:

Started PATCH "/guardar_asistencia" for localhost at 2023-06-30 17:02:40 -0500
Processing by AsistenciaController#update as TURBO_STREAM
  Parameters: {"authenticity_token"=>"[FILTERED]", "id"=>"1", "username"=>"Prueba", "entry"=>"false", "commit"=>"Registrar"}
  [1m[36mUser Load (0.2ms)[0m  [1m[34mSELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?[0m  [["id", 1], ["LIMIT", 1]]
  ↳ app/controllers/users_controller.rb:63:in `set_user'
Completed 400 Bad Request in 29ms (ActiveRecord: 1.9ms | Allocations: 5996)


  
ActionController::ParameterMissing (param is missing or the value is empty: asistencia):
  
app/controllers/asistencia_controller.rb:16:in `asistencia_params'
app/controllers/asistencia_controller.rb:3:in `update'
Started GET "/asistencia/asistencia" for localhost at 2023-06-30 17:02:42 -0500
Processing by AsistenciaController#asistencia as HTML
  Rendering layout layouts/application.html.erb
  Rendering asistencia/asistencia.html.erb within layouts/application
  Rendered asistencia/asistencia.html.erb within layouts/application (Duration: 2.1ms | Allocations: 3438)
  Rendered layout layouts/application.html.erb (Duration: 57.6ms | Allocations: 17125)
Completed 200 OK in 65ms (Views: 60.9ms | ActiveRecord: 0.0ms | Allocations: 17673)
1

There are 1 best solutions below

9
Les Nightingill On

Update: Now that you have shared your entire repo, I can see a number of problems. I don't know what you're trying to do so I can't resolve them all!

  1. the asistencia method in AsistenciaController is designated as private and it's not used. Rails is just rendering the view.
  2. You initialize the @asistencia variable as User.all. This make no sense, it should be a single User instance.
  3. AsistenciaController inherits from UsersController. I'm not sure why. It's unusual.
  4. The definition of asistencia_params is incorrect. The @asistencia variable is an instance of User, so the params hash is of the form {user: {id: 1, username: 'Harry', entry: "true"}}. So the strong parameters definition in UsersController will work.
class AsistenciaController < UsersController
  def update
    @asistencia = User.find(user_params[:id])
    if @asistencia.update(user_params)
      redirect_to asistencia_path, notice: "Asistencia guardada correctamente"
    else 
      render :edit, status: :unprocessable_entity
    end
  end

  def asistencia
   @asistencia = User.new
  end

end

This gives an error due to the fact that there's no user id to set the user for the update action (set_user method in UsersController). I don't know how you plan to do this.