6

I am trying to update the attribute on clicking the button block or unblock. but its not getting updated in database and i get the error param is missing or the value is empty: user . even when the user id is passed i get this error. I am not able to find where my error is occuring

This is my controller code:

class UserController < ApplicationController
    before_action :set_user, only: [ :edit, :update]

    # GET /users
    # GET /users.json
    def index
      @users = User.all
    end

    def edit
    end

    def update
      respond_to do |format|
        if @user.update(user_params)
          format.html { redirect_to user_index_path, notice: 'User was successfully blocked.' }
        else
          format.html { render :index }
        end
      end
    end

    private
      # Use callbacks to share common setup or constraints between actions.
      def set_user
        @user = User.find(params[:id])
      end

      # Never trust parameters from the scary internet, only allow the white list through.
      def user_params
        params.require(:user).permit(:active)
      end
  end

This is my view code

<h1>
  User Details
</h1>
<table class="table">
  <thead>
    <tr>
      <th>Users</th>
    </tr>
  </thead>
  <tbody>
    <% @users.each do |user| %>
      <tr>
        <td>
          <%= image_tag("/assets/image.jpg", style: "width: 60px; height: 60px;")%>
        </td>
        <td>
          <p class="font-weight-bold"><%= user.name %></p>
          <p><%= user.email %></p>
        </td>
        <td><%= user.active %></td>
        <td>
        <% if user.active == true  %>
        <%= button_to "Block", user_path(id: user.id, value: false), class: 'btn btn-outline-dark',  :method => :patch %>
        <%else%>
        <%= button_to "Unblock", user_path(id: user.id, value: true), class: 'btn btn-outline-dark',  :method => :patch %>
        <%end%>
      </td>
      </tr>
    <% end %>
  </tbody>
</table>

Terminal log i get

Started PATCH "/user/5?value=false" for 127.0.0.1 at 2018-04-12 14:49:57 +0530
    Processing by UserController#update as HTML
      Parameters: {"authenticity_token"=>"RyL/KkxIbVtZXojmERByarlN1qxXH0gK7moq2w5s4ULketOYGh1im+qPLvND2HkOlVC8gePlFyGCns0lf/Z/uQ==", "value"=>"false", "id"=>"5"}
      User Load (0.3ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 5 ORDER BY `users`.`id` ASC LIMIT 1
      User Load (0.2ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 5 LIMIT 1
    Completed 401 Unauthorized in 3ms (ActiveRecord: 0.6ms)



    ActionController::ParameterMissing - param is missing or the value is empty: user:
      app/controllers/user_controller.rb:31:in `user_params'
      app/controllers/user_controller.rb:15:in `block in update'
      app/controllers/user_controller.rb:14:in `update'

    Started POST "/__better_errors/7f240c65d5c4ef9a/variables" for 127.0.0.1 at 2018-04-12 14:49:57 +0530
1
  • In your user_params method, permit all parameters, params.require(:user).permit! or specify the parameters one by one. Commented Apr 12, 2018 at 9:43

3 Answers 3

4

You require user param with active field:

  def user_params
    params.require(:user).permit(:active)
  end 

You send just value param. You need to change your button_to code to sth like

button_to("Block", user_path(id: user.id), 
   class: 'btn btn-outline-dark',  :method => :patch, params: { user: { active: true}}

(or active: false respectively).

Sign up to request clarification or add additional context in comments.

Comments

4

This error comes up when we don't pass the parameter from view properly. In your case the parameter should come inside the user hash.

In your private section:-

def user_params
  params.require(:user).permit(:active)
end

You are actually expecting the parameters to be enclosed with user hash.

And as you are expecting the active attribute inside the user hash, it is giving you the error as param is missing or the value is empty: user.

Which means it is looking for the active attribute, which is missing in the incoming parameter.

For example, the parameter which should be hitting your controller should be something like this:-

{"user"=>{"active"=>"true"}, "value"=>"false", "id"=>"5"}

Pass the active parameter from your view page within the user hash and the error will be gone. Moreover you can always learn more about the strong parameters.

Comments

1

In your method user_params you require the param user. The links on the buttons only have the get param value, but not user.

The get param should be called sth. like user[active].

Your block button should look like this:

<%= button_to "Block", user_path(id: user.id, 'user[active]': false), class: 'btn btn-outline-dark',  :method => :patch %>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.