colio303 colio303 - 1 month ago 6
Ruby Question

Ruby on Rails; Form id tag Will not Update

I am new to rails and I recently started a new Rails project where I am attempting to use an existing database. While creating the model for this database I used some symbols

(:emid, :sid, :conid, :emnum, :tc, :emac)
in my
new.html.erb
file to identify the form fields. I did not realize that these had to match the names of the database columns. I changed them to what they are now
("employee_id", "supplier_id", "contractor_id", "employee_number", "trade_code", "employee_active")
, but the html page that loads does not change. The id attribute for the inputs of the form will not update even though I've changed all of them.

Model (employee.rb):

class Employee < ActiveRecord::Base
self.table_name = "Employee"
def change
create_table "Employee", primary_key: :"employee_id", force: :cascade do |t|
t.integer "supplier_id", limit: 4, null: false
t.integer "contractor_id", limit: 4, null: false
t.string "employee_number", limit: 45, null: false
t.string "trade_code", limit: 45, null: false
t.integer "employee_active", limit: 1, default: 1, null: false
end
end
end


new.html.erb:

<p>
<%= f.label :title %><br>
<%= f.text_field "employee_id" %>
</p>

<p>
<%= f.label :text %><br>
<%= f.text_field "supplier_id" %>
</p>

<p>
<%= f.label :text %><br>
<%= f.text_field "contractor_id" %>
</p>

<p>
<%= f.label :text %><br>
<%= f.text_field "employee_number" %>
</p>

<p>
<%= f.label :text %><br>
<%= f.text_field "trade_code" %>
</p>

<p>
<%= f.label :text %><br>
<%= f.text_field "employee_active" %>
</p>


Error output from browser:

{"utf8"=>"✓",
"authenticity_token"=>"y9UsuK6rc9qdhy/e0WPUJiT5rzw1QQqDuGpIVl4l4sUpVP2ZUm6Yak3/GImT8niSt5wbyf43OUGH1FrzulylQA==",
"employee"=>{"emid"=>"",
"sid"=>"666",
"conid"=>"66",
"emnum"=>"f",
"tc"=>"f",
"emac"=>"1"},
"commit"=>"Save Employee"}


Controller:

class EmployeesController < ApplicationController
def new

end
def create
@employee = Employee.new(e_params)

@employee.save
redirect_to @employee
end
private
def e_params
params.require(:employee).permit("employee_id", "supplier_id", "contractor_id", "employee_number", "trade_code", "employee_active")
end
end


HTML Source:

<p>
<label for="employee_title">Title</label><br>
<input type="text" name="employee[emid]" id="employee_emid" />
</p>

<p>
<label for="employee_text">Text</label><br>
<input type="text" name="employee[sid]" id="employee_sid" />
</p>

<p>
<label for="employee_text">Text</label><br>
<input type="text" name="employee[conid]" id="employee_conid" />
</p>

<p>
<label for="employee_text">Text</label><br>
<input type="text" name="employee[emnum]" id="employee_emnum" />
</p>

<p>
<label for="employee_text">Text</label><br>
<input type="text" name="employee[tc]" id="employee_tc" />
</p>

<p>
<label for="employee_text">Text</label><br>
<input type="text" name="employee[emac]" id="employee_emac" />
</p>


Because of this, I get the error:

Mysql2::Error: Field 'supplier_id' doesn't have a default value: INSERT INTO ``Employee`` VALUES ()


I am assuming this is because my model can't recognize what the html forms are and can't get the information from it.

How do I get the id attributes to update to what they are now?

Answer

I manage to get around this by closing everything and restarting my computer. I'm sure it wasn't the best way to to this, but it got the job done. When I started the server again and checked the id tags from page source they all had the updated values from the new.html.erb file.