lundi 24 octobre 2016

undefined method `gsub' for nil:NilClass or how to use gsub method in rails

I want to generate an Employee_ID with serially, I have fixed a initial employee id in database table. the format is "E36162000", I am taking last employee id from table then extract only integer value then add one, this will be next employee id.

But in this gsub() method is not working, gsub method extract integer is perfectly, link Next Employee_id = E36162001

But when am i submit then error comes. I am sending code and screen sort please help me

_employee_details.html.erb this is my view

<div class="modal-body">
    <h2 class="text-center">Add <span>Employee Details</span></h2>
    <div class="post-new-job head-border">
      <div class="alert alert-success" role="alert" id='success-job' style='display:none;'>Employee Details is successfully added.</div>
        <div class="form-body">             
            <%= form_for(:employee_details, :url => {:controller => 'hr', :action => 'create'}) do |f| %>                   
                <div class="col-md-12">
                <!--auto generate emp id-->
                <% @last_emp_id = EmployeeDetail.select("employee_id").last %>
                <% emp_id = @last_emp_id.employee_id  %>
                <% emp_id_sub = emp_id.gsub(/[^\d]/, '') %>
                <% auto_generate_id = 'E'.to_s + (emp_id_sub.to_i + 1).to_s %>
                <h1> Employee ID : <%= auto_generate_id %> </h1>


                <div class="mydata">
                 <%= f.hidden_field :offer_letter_id, { class: 'form-control', id: 'recipient-name' } %>
                </div>

                 <div class="form-group">
                    <label class="control-label">Employee ID</label>
                    <div class="input-group"> <span class="input-group-addon"> <i class="fa fa-user"></i> </span>
                      <%= f.text_field :employee_id, { :value => auto_generate_id, :disabled=>true , :required => true, placeholder: 'E12345678', class: 'form-control' } %>
                    </div>
                  </div>

                  <div class="form-group">
                    <label class="control-label">Bank Account</label>
                    <div class="input-group"> <span class="input-group-addon"> <i class="fa fa-university"></i> </span>
                      <%= f.text_field :bank_ac, {placeholder: '06464060852634865', class: 'form-control' } %>
                    </div>
                  </div>

                  <div class="form-group">
                    <label class="control-label">Bank IFSC Code</label>
                    <div class="input-group"> <span class="input-group-addon"> <i class="fa fa-code"></i> </span>
                      <%= f.text_field :bank_ifsc, {placeholder: 'SBI012356', class: 'form-control' } %>
                    </div>
                  </div>

                  <div class="form-group">
                    <label class="control-label">End of Date</label>
                    <div class="input-group"> <span class="input-group-addon"> <i class="fa fa-calendar"></i> </span>
                      <%= f.text_field :work_end_date, {  placeholder: 'MM/DD/YYYY', id: 'datepicker', class:"datepicker_style" } %>
                    </div>
                  </div>

                  <div class="form-group">
                    <label class="control-label">Gender</label>
                    <div class="input-group"> <span class="input-group-addon"> <i class="fa fa-male fa-female"></i> </span>
                      <%= f.select :gender, ['Male', 'Female'], { }, class: "form-control" %>
                    </div> 
                  </div>

                  <div class="form-group">
                    <label class="control-label">Spouse Name</label>
                    <div class="input-group"> <span class="input-group-addon"> <i class="fa fa-user"></i> </span>
                      <%= f.text_field :spouse_name, { placeholder: 'Father/Mother/Wife name', class: "form-control" } %>
                    </div>
                  </div> <br>

                  <div class="form-group">
                    <a><%= f.submit "Add Employee Details", :class => "btn btn-primary" %></a> 
                  </div>
                </div>
                <div class="col-md-2"></div>                
            <%- end -%>
        </div>      
    </div>
   </div>

employee_details.rb this is my model

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

end

hr_controller.rb this is my controler

class HrController < ApplicationController
def new
    @employees = EmployeeDetail.new
end

# edit employee information
def edit
    @employees = EmployeeDetail.find(params[:id])
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

def show
    @employees = EmployeeDetail.find(params[:id])
end

def update
    @employees = EmployeeDetail.find(params[:id])

    if @employees.update(employee_params)
        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

my layout View enter image description here

**Error in this line **

<% emp_id_sub = emp_id.gsub(/[^\d]/, '') %>

error is enter image description here

When am i Click on submit button then error is generate but data is save in employee_details table except Employee_Id

Aucun commentaire:

Enregistrer un commentaire