Home > Back-end >  ActiveRecord::RecordNotFound in WorkersController#create
ActiveRecord::RecordNotFound in WorkersController#create

Time:09-29

now i'm doing creating a worker with role. I have worker , role and worker_role. Now i have done the create worker form and have a selection to choose role. After done the form i need to role_id and worker_id store in worker_role table. I have try and it return me some error.

worker controller

def new
 @worker = Worker.new
 @worker.company_id = params[:company_id]
 @role = Role.all
end

def create
  @worker = Worker.new(worker_params)
  @company_id = Company.find(params[:worker][:company_id])
  @role_id = Role.all
  if @worker.save
   @worker_role = Role.find(params[:id]).worker_roles.create(worker: worker, returned: true)
   #log_in @worker
   flash[:success] = "Welcome to the Areca Supermarket System!"
   redirect_to @worker
 else
   render 'new'
 end
end

new.html.erb(the form)

<% provide(:title, "Register worker") %>
<h1>Create Worker</h1>

<div >
  <div >
    <%= form_with(model: @worker, local: true) do |f| %>
    <%= render 'shared/error_messages', object: f.object %>

  <%= f.label :name %>
  <%= f.text_field :name, class: 'form-control' %>

  <%= f.label :email %>
  <%= f.email_field :email, class: 'form-control' %>

  <%= f.label :phone_number %>
  <%= f.number_field :phone_number, class: 'form-control' %>

  <%= f.label :address %>
  <%= f.text_field :address, class: 'form-control' %>

  <%= f.label :password %>
  <%= f.password_field :password, class: 'form-control' %>

  <%= f.label :role_id %>
  <%= f.select(:role_id, Role.all.collect { |l| [ l.name, l.id] }, {class: "form-select"}) %>

  <%= f.hidden_field :company_id , value: 2%>


   <%= f.submit "Create worker", class: "btn btn-primary" %>
 <% end %>
 </div>
</div>

worker_role model

class WorkerRole < ApplicationRecord
 belongs_to :worker
 belongs_to :role
end

worker_role migration

create_table "worker_roles", force: :cascade do |t|
 t.integer "worker_id"
 t.integer "role_id"
 t.datetime "created_at", null: false
 t.datetime "updated_at", null: false
end

worker migration

create_table "workers", force: :cascade do |t|
 t.string "name"
 t.string "email"
 t.integer "phone_number"
 t.string "address"
 t.integer "company_id"
 t.datetime "created_at", null: false
 t.datetime "updated_at", null: false
 t.string "password_digest"
 t.boolean "admin", default: false
end

Error show in website

parameter in website

Update

Error show(supplier)

create_table "outlet_suppliers", force: :cascade do |t|
 t.integer "outlet_id"
 t.integer "supplier_id"
 t.datetime "created_at", null: false
 t.datetime "updated_at", null: false

end

CodePudding user response:

Hi as mentioned in request params, I can see that role_id you are passing in params as

Parameters:

params = {"authenticity_token"=>"[FILTERED]",“worker"=>{"name"=>"Daniel", "“email"=:“[email protected]", “phone_number"=:"Q134657891",“address"=>"Alor Setar", “password"=>"[FILTERED]", “role id"=>"1", “company _id"=:"2",“commit"=>"Create worker"}

#you can access the role ID as below:  
params[:worker][:role_id]

But you are using it in the controller as

@worker_role = Role.find(params[:id])

#this should be as below: 
@worker_role = Role.find(params[:worker][:role_id]).worker_roles.create(worker: @worker)

# OR 
#if you are permitting role id
@worker_role = Role.find(worker_params[:role_id]).worker_roles.create(worker: @worker)
  
  • Related