Home > Back-end >  Rails: Param is missing or the value is empty in ManagerController#addtoproject
Rails: Param is missing or the value is empty in ManagerController#addtoproject

Time:02-12

I have been trying to add project_id and user_id to the following

 create_table "project_users", force: :cascade do |t|
    t.integer "project_id"
    t.integer "user_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

Model project_user.rb

class ProjectUser < ApplicationRecord
  has_many :users
  has_many :projects
end

This is my controller that is supposed to add data in the table:

def addtoproject
    @emp_id = params[:eid]
    @project_id = params[:pid]

    @add_user = ProjectUser.new(params.require(:projectuser).permit(:project_id, :user_id))
    @add_user.project_id = @project_id
    @add_user.user_id = @emp_id
    @add_user.save
  end

@emp_id and @project_id both contain valid values. Rails is throwing the error on the line

@add_user = ProjectUser.new(params.require(:projectuser).permit(:project_id, :user_id))

Error screenshot:

enter image description here

Any clues how this error can be fixed? As the table name is "project_users", I reckon I maybe missing an "_" somewhere?

Update 1 Clicking Add to Project invokes the method addtoproject inside controller.

enter image description here

addusers.html.erb:

<table id='employee-table' >
    <thead  >
      <tr>
        <th>Employee ID</th>
        <th>Name</th>
        <th>Role</th>
        <th colspan="2"></th>
      </tr>
    </thead>

    <tbody>
      <% @employees.each_with_index do |employee, index| %>
        <tr>
          <td><%= employee.id %></td>
          <td><%= employee.firstname   ' '   employee.lastname %></td>
          <td><%= employee.user_type %></td>
          <td><%= link_to "Add to Project", add_to_project_path(eid: employee, pid: @project_id), :class => "btn btn-success", :style => "float:right"%></td>
        </tr>
      <% end %>
    </tbody>
  </table>

CodePudding user response:

It's complaining because you're doing

params.require(:projectuser)

This means that :projectuser doesn't exist in params. You can see the parameters that are being sent to your controller further down in that screenshot. Only a pid and eid are present.

If you're using a form, you may need to change it to use something like form_for so your parameters are namespaced to the model.

<%= form_for @project_user do |f| %>
  <%= f.number_field :pid %>
  <%= f.number_field :eid %>

You'll also face errors with the `permit section since you specify

permit(:project_id, :user_id)

But the params passed are pid and uid. Use the name of your model fields in your inputs:

<%= form_for @project_user do |f| %>
  <%= f.number_field :project_id %>
  <%= f.number_field :user_id %>

CodePudding user response:

It turns out that if the parameters are not passed using form_with as in my case, there is no need for params.require or params.permit (as quoted by @Cjmarkham these attributes are not present).

Just initialising an empty instance of the model and saving it worked for me.

@add_user = ProjectUser.new
@add_user.project_id = @project_id
@add_user.user_id = @emp_id
@add_user.save
  • Related