JITENDRA KUSHVAHA JITENDRA KUSHVAHA - 1 month ago 5
Ruby Question

how to set value in text_field and store these value into database table in rails

These things i have all ready done, but at that time the value of text_field is not insert into database table, reaming value insert successfully but one value is not inserted.

I can understand where am i wrong and what is missing...

_employee_details.html.erb

<%= form_for(:employee_details, :url => {:controller => 'hr', :action => 'create'}) do |f| %>
<%= f.text_field :employee_id, { :value=> "123548", :disabled=>true , :required => true, placeholder: 'E12345678', class: 'form-control' } %>
<% end %>


hr_controller.rb

class HrController < ApplicationController

def new
@employees = EmployeeDetail.new
end

def create
@employees = EmployeeDetail.new(employee_params)
if @employees.save
redirect_to :action => 'internal_employee_page'
else
redirect_to :action => 'internal_employee_page'
end
end

private

def employee_params
params.require(:employee_details).permit(:offer_letter_id, :employee_id, :bank_ac, :bank_ifsc, :spouse_name, :gender, :work_end_date)
end
end


All value is successfully insert into employee_details table accept employee_id

employee_details.rb this is my modal

class EmployeeDetail < ActiveRecord::Base
belongs_to :user
validates :offer_letter_id, presence: true
end


I have already try this but this is not working




Answer

Disabled input will not submit data.

Instead of disabled Use the readonly attribute:

<%= f.text_field :employee_id, { :value=> "123548", :readonly => true , :required => true, placeholder: 'E12345678', class: 'form-control' } %>

<%= form_for(:employee_details, :url => {:controller => 'hr', :action => 'create'}) do |f| %>
       <%= f.text_field :employee_id, { :value=> "123548", :readonly => true , :required => true, placeholder: 'E12345678', class: 'form-control' } %>
<% end %>

Here is the documentation of it

Comments