Training Force Training Force - 5 months ago 8
Ruby Question

Collection_select not passing values into insert

I am having a problem saving data from a dropdown
Below I have the fields on form and logs that go with it

This set works fine as you can see by the log inserts

<div class="field">
<%= f.label :company_id %><br>
<%= f.number_field :company_id %>
</div>


Log file

INSERT INTO "learners"
("learner_id", "company_id", "id_type", "id_number", "first_name", "last_name", "middle_names", "title", "equity", "gender", "date_of_birth", "created_at", "updated_at")
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
[["learner_id", 1], ["company_id", 1], ["id_type", "Book"], ["id_number", 33], ["first_name", "Jack"], ["last_name", "Pillay"], ["middle_names", ""], ["title", "Mr"],
["equity", "Black"], ["gender", "Male"], ["date_of_birth", "2016-06-28"],
["created_at", "2016-06-28 19:16:41.969037"], ["updated_at", "2016-06-28 19:16:41.969037"]]


Dropdown works but no values are being passed

<div class="field">
<%= f.label :company_id %><br>
<%= collection_select(:learner, :learner_id, Client.all, :company_id, :name, prompt: true) %>
</div>


Log File - See missing company_id

INSERT INTO "learners"
("learner_id", "id_type", "id_number", "first_name", "last_name", "middle_names", "title", "equity", "gender", "date_of_birth", "created_at", "updated_at")
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
[["learner_id", 1], ["id_type", "Book"], ["id_number", 33], ["first_name", "Jack"], ["last_name", "Pillay"], ["middle_names", ""], ["title", "Mr"],
["equity", "Black"], ["gender", "Male"], ["date_of_birth", "2016-06-28"],
["created_at", "2016-06-28 19:16:41.969037"], ["updated_at", "2016-06-28 19:16:41.969037"]]


Learners controller

class LearnersController < ApplicationController
before_action :set_learner, only: [:show, :edit, :update, :destroy]

# GET /learners
# GET /learners.json
def index
@learners = Learner.all
end

# GET /learners/1
# GET /learners/1.json
def show
end

# GET /learners/new
def new
@learner = Learner.new
@clients = Client.all
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @learner }
end
end

# GET /learners/1/edit
def edit
end

# POST /learners
# POST /learners.json
def create
@learner = Learner.new(learner_params)

respond_to do |format|
if @learner.save
format.html { redirect_to @learner, notice: 'Learner was successfully created.' }
format.json { render :show, status: :created, location: @learner }
else
format.html { render :new }
format.json { render json: @learner.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /learners/1
# PATCH/PUT /learners/1.json
def update
respond_to do |format|
if @learner.update(learner_params)
format.html { redirect_to @learner, notice: 'Learner was successfully updated.' }
format.json { render :show, status: :ok, location: @learner }
else
format.html { render :edit }
format.json { render json: @learner.errors, status: :unprocessable_entity }
end
end
end

# DELETE /learners/1
# DELETE /learners/1.json
def destroy
@learner.destroy
respond_to do |format|
format.html { redirect_to learners_url, notice: 'Learner was successfully destroyed.' }
format.json { head :no_content }
end
end

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

# Never trust parameters from the scary internet, only allow the white list through.
def learner_params
params.require(:learner).permit(:learner_id, :company_id, :id_type, :id_number, :date_of_birth, :first_name, :last_name, :middle_names, :title, :equity, :gender)
end
end

Answer

This line

<%= collection_select(:learner, :learner_id, Client.all, :company_id, :name, prompt: true) %>

tells Rails to send the selected value to the server in params[:learner][:learner_id]. When you want to use that value as the client_id you should change that line to:

<%= collection_select(:learner, :company_id, Client.all, :company_id, :name, prompt: true) %> 

Or use the FormBuilder version that is a bit shorter:

<%= f.collection_select(:company_id, Client.all, :company_id, :name, prompt: true) %>