如何在不同的控制器中调用相同的方法?

时间:2017-10-03 07:08:05

标签: ruby-on-rails

我有一个表空位主人它有创建方法。我在经理自助服务中做了同样的表格。我想调用相同的方法来创建用于空缺主表单的表单。那么,我如何在经理自助服务中使用相同的方法。

表格 -

<div class="box box-default">
  <div class="box-header with-border">
    <h3 class="box-title">Requisition Form</h3>
  </div><!-- /.box-header -->
  <div class="box-body">
  <%= bootstrap_form_for(:vacancy_master,url: { action: 'vacancy_request'},html: {id: 'vacancy_master'}) do |f| %>
  <%= f.hidden_field :employee_id, :value => current_user.employee_id %>
  <%= f.hidden_field :vacancy_post_date, value: Date.today %>
   <%= f.hidden_field :company_id, :value => current_user.company_id  %>
   <%= f.hidden_field :company_location_id, :value => current_user.company_location_id  %>
    <%= f.hidden_field :department_id, :value => current_user.department_id  %>
      <div class="row">
       <div class="col-sm-3">
         <div class="form-group required">
          <!--   <label>Job Title</label> -->
            <%= f.select :employee_designation_id,all_employee_designation,{label: 'Designation',include_blank: " Select Designation"} %>
          </div>
        </div>

      <div class="col-sm-3">
         <div class="form-group required">
          <!--   <label>Job Title</label> -->
            <%= f.select :cost_center_id,all_cost_center,{label: 'Cost Center',include_blank: " Select Cost Center"} %>
          </div>
        </div>

        <div class="col-sm-3">
          <div class="form-group required">
           <%= f.text_field :vacancy_name,label: 'Vacancy Name'%>
          </div>
        </div>

         <div class="col-sm-3">
          <div class="form-group required">
            <%= f.select :vacancy_type,["Permanent","Contract","Freelancer","Internship"],{label: 'Vacancy Type',include_blank:"Select vacancy Type"}%>
          </div>
        </div> 
      </div>

       <div class="row">
        <div class="col-sm-3">
          <div class="form-group required">
             <%= f.text_field :experience,label: 'Minimum Experience'%>
          </div>
        </div>

        <div class="col-sm-3">
          <div class="form-group required">
             <%= f.text_field :experince_max,label: 'Maximum Experience'%>
          </div>
        </div> 

        <div class="col-sm-3">
          <div class="form-group required">
             <%= f.text_field :budget,label: 'Minimum Budget'%>
          </div>
        </div> 

        <div class="col-sm-3">
          <div class="form-group required">
             <%= f.text_field :budget_max,label: 'Maximum Budget'%>
          </div>
        </div> 

        </div>

         <div class="row">

           <div class="col-sm-3">
          <div class="form-group required">
             <%= f.text_field :no_of_position,label: 'No. of Positions'%>
          </div>
        </div> 

          <div class="col-sm-3">
          <div class="form-group required">

             <%= f.select :reason,["New","Replacement","Bench"],{label: 'Reason for the Requirement',include_blank:"Select Reason"},{onchange:"var a={reason:$(this).val()}; $.get('/vacancy_masters/select_reason',a,function(response){});",class: 'form-control'} %>

          </div>
        </div> 

        <div id="replacement_textbox"></div>

         <div class="col-sm-3">
          <div class="form-group required">

             <%= f.select :notice_period,[['Yes','true'],['No','false']],{label: 'Notice Period',include_blank:"Select Option"},{onchange:"var a={notice_period:$(this).val()}; $.get('/vacancy_masters/select_notice_period',a,function(response){});",class: 'form-control'}%>
          </div>
        </div> 

        <div id="notice_period_textbox"></div>

         </div>
        <div class="row">

         <div class="col-sm-3">
          <div class="form-group required">
             <%= f.text_field :keyword,label: 'Primary Skill'%>
          </div>
        </div> 

        <div class="col-sm-3">
          <div class="field">
             <%= f.text_field :secondary_skill,label: 'Secondary Skill'%>
          </div>
        </div> 

         <div class="col-sm-3">
          <div class="form-group required">

             <%= f.select :relocation_rerimbursement,[['Yes','true'],['No','false']],{label: 'Relocation Reimbursment',include_blank:"Select Option"},{onchange:"var a={relocation_rerimbursement:$(this).val()}; $.get('/vacancy_masters/select_relocation',a,function(response){});",class: 'form-control'} %>
          </div>
        </div> 

         <div id="relocation_textbox"></div>





        </div>

      <div class="row">
               <div class="col-sm-3">
          <div class="form-group required">        
            <div class="input-group">
              <div class="input-group-addon">
                <i class="fa fa-calendar"></i>
              </div>
               <%= f.text_field :vacancy_fullfillment_date, label: 'Vacancy Fulfillment Date', class: 'birth_date'%>
            </div>
          </div>
        </div> 

          <div class="col-sm-3">
         <div class="form-group required">
            <div class="input-group">
              <div class="input-group-addon">
                <%= link_to "", new_target_company_path,class: "fa fa-plus fa-success",:target => "_blank" %>
              </div>
          <!--   <label>Job Title</label> -->
            <%= f.select :target_company_id,all_target_company,{label: 'Target Company',include_blank: " Select Target Company"} %>
            </div>
          </div>
        </div>

        <div class="col-sm-3">
         <div class="form-group required">
          <!--   <label>Job Title</label> -->
            <%= f.select :billable,["Billable","Non Billable"],{label: 'Billable/Non Billable',include_blank: " Select Resource"} %>
          </div>
        </div>

         <div class="col-sm-3">
         <div class="form-group required">
          <!-- <label>Qualification I</label> -->
            <div class="input-group">
              <div class="input-group-addon">
                <%= link_to "", new_degree_path,class: "fa fa-plus fa-success",:target => "_blank" %>
              </div>
               <%= f.select :degree_id, all_degree, {label: 'Qualification I',include_blank: " Select Qualification"}, class: 'input_degree_stream_id' %>
            </div>
          </div>
        </div>     


      </div>

          <div class="row">
           <div class="col-sm-3">
          <label>Qualification II</label>
          <div class="field">
            <div class="input-group">
              <div class="input-group-addon">
                <%= link_to "", new_degree_path,class: "fa fa-plus fa-success",:target => "_blank" %>
              </div>
               <%= f.select :degree_1_id, all_degree, {hide_label: true,include_blank: " Select Qualification"}, class: 'input_degree_stream_id' %>
            </div>
          </div>
        </div>


              <div class="col-sm-3">
          <label>Qualification III</label>
          <div class="field">
            <div class="input-group">
              <div class="input-group-addon">
                <%= link_to "", new_degree_path,class: "fa fa-plus fa-success",:target => "_blank" %>
              </div>
               <%= f.select :degree_2_id, all_degree, {hide_label: true,include_blank: " Select Qualification"}, class: 'input_degree_stream_id' %>
            </div>
          </div>
        </div>
        </div>

      <div class="row">
        <div class="col-sm-12">
          <div class="field">
           <%= f.text_area :description,label: 'Job Description',placeholder: "e.g : Any Description related to Vacancy"%>
          </div>
        </div>


      <!--   <div class="col-sm-3">
          <div class="form-group required">
           <label>Vacancy Approval</label>
          <= f.select :reporting_master_id,all_recruitment_reporting_master_list,{label: 'Vacancy Approval',include_blank: " Select Reporting Person"},{:class=>"select2 select2-hidden-accessible",:style=>"width: 100%;",:tabindex=>"-1"} %>
            </div>
        </div> -->

       </div>  


      <div class="row">
        <div class="col-sm-12">
          <div class="field">
           <%= f.text_area :justification,label: 'Justification',placeholder: "e.g : Any Description related to why its needed for creating this vacancy"%>
          </div>
        </div>
      </div> 

      <div class="row">
        <div class="col-sm-3">
          <div class="actions">
            <%= f.submit "Initiate Requisition",class: "btn btn-primary btn-sm" %>
            <%= link_to 'Back', vacancy_masters_path,class:"btn btn-sm btn-default fa fa-arrow-left" %>
          </div>
        </div>
      </div>
    </div>
  </div>
<% end %>

<script type="text/javascript">
  $(function(){
    $('.birth_date').datepicker({
    changeYear:true,
    changeMonth: true,
    yearRange: '-200:-0',
    dateFormat: 'dd-mm-yy' });
});



  $('#vacancy_master_reason').on('change', function() {
    if ($('#vacancy_master_reason').val() == "Replacement"){

      $("#replacement_textbox").show();
    }
    else{
      $("#replacement_textbox").hide();
    }
  });


   $('#vacancy_master_notice_period').on('change', function() {
    if ($('#vacancy_master_notice_period').val() == "true"){

      $("#notice_period_textbox").show();
    }
    else{
      $("#notice_period_textbox").hide();
    }
  });
    $('#vacancy_master_relocation_rerimbursement').on('change', function() {
    if ($('#vacancy_master_relocation_rerimbursement').val() == "true"){

      $("#relocation_textbox").show();
    }
    else{
      $("#relocation_textbox").hide();
    }
  });
</script>

空缺大师的控制器方法 -

def create
    @vacancy_master = VacancyMaster.new(vacancy_master_params)

    if params[:rep_flag] == "Replacement"
      @vacancy_master.replacement_id = params[:common][:replacement_id]
      @replacement_textbox = true
    else
      @replacement_textbox = false
    end

    if params[:flag] == "true"
      @vacancy_master.notice_period_day = params[:common][:notice_period_day]
      @notice_period_textbox = true
    else
      @notice_period_textbox = false
    end

    if params[:flag] == "true"
      @vacancy_master.relocation_cost = params[:common][:relocation_cost]
      @relocation_textbox = true
    else
      @relocation_textbox = false
    end

    a=current_user.employee_id
    employee = Employee.where(id: a).take
    if employee.try(:manager_id).nil?
        flash[:alert] = "Reporting Manager not set please set Reporting Manager"
        redirect_to new_vacancy_master_path
      else
        @vacancy_master.current_status = "Pending"
        @vacancy_master.reporting_master_id = employee.manager_id
        respond_to do |format|
      if @vacancy_master.save
        dept_id = params[:employee][:department_id]
        location = params[:employee][:company_location_id]
        company = params[:vacancy_master][:company_id]
        VacancyMaster.where(id: @vacancy_master.id).update_all(company_id: company,company_location_id: location,department_id: dept_id)
        ReportingMastersVacancyMaster.create(reporting_master_id: current_user.employee_id, vacancy_master_id: @vacancy_master.id,vacancy_status: "Pending")
        for i in 1..@vacancy_master.no_of_position.to_i
          ParticularVacancyRequest.create(vacancy_master_id: @vacancy_master.id, employee_id: @vacancy_master.employee_id, open_date: @vacancy_master.vacancy_post_date, fulfillment_date: @vacancy_master.vacancy_fullfillment_date,status: "Pending", employee_designation_id: @vacancy_master.employee_designation_id, vacancy_name: @vacancy_master.vacancy_name)
        end
vacancy_name: @vacancy_master.vacancy_name,no_of_position: @vacancy_master.no_of_position,description: @vacancy_master.description,vacancy_post_date: @vacancy_master.vacancy_post_date,budget: @vacancy_master.budget,department_id: @vacancy_master.department_id,employee_designation_id: @vacancy_master.employee_designation_id,company_location_id: @vacancy_master.company_location_id,degree_id: @vacancy_master.degree_id,degree_1_id: @vacancy_master.degree_1_id,degree_2_id: @vacancy_master.degree_2_id,experience: @vacancy_master.experience,keyword: @vacancy_master.keyword,other_organization: @vacancy_master.other_organization,industry: @vacancy_master.industry,reporting_master_id: @vacancy_master.reporting_master_id,current_status: @vacancy_master.current_status,employee_id: @vacancy_master.employee_id,justification: @vacancy_master.justification)
        VacancyMasterMailer.vacancy_request(@vacancy_master).deliver_now

        format.html { redirect_to @vacancy_master, notice: 'Vacancy Created Successfully.' }
        format.json { render :show, status: :created, location: @vacancy_master }
      else
        format.html { render :new }
        format.json { render json: @vacancy_master.errors, status: :unprocessable_entity }
      end
    end
  end
end

其他控制器代码 -

 def vacancy_request
    @vacancy_master = VacancyMaster.new
    @vacancy_masters = VacancyMaster.where(employee_id: current_user.employee_id)

1 个答案:

答案 0 :(得分:0)

如果要使用相同的HTML表单,则在其他控制器操作中,您可以使用rails partial templates rails doc

如果要在第二个控制器中为该表单使用相同的控制器代码,可以在应用程序控制器中使用通用方法并在其中粘贴所有create方法代码,然后从两个控制器调用该方法。

此外,您可以使用rails helpers在其中拥有公共代码,并从控制器中调用它们。