简体   繁体   中英

how to pass argument from form_for method to controller method in rails?

I have a bootstrap modal, when i clicked on the button then this bootstrap model will be called (open popup menu) and pass different-different offer_id's .

this popup menu open a form(like edit form), I edited all information then click on edit button then only first records is updated.

for example I want to edit information of offer_id = 1 , when fill all form and submit then offer_id = 1 is updated not offer_id = 5 .

so i don't know exactly where am i wrong, and what am i missing..

edit button and model on the same page ( _employee_details.html.erb )

edit button

<div class="edit-recr-wrp1"> 
       <button type="button" class="btn btn-primary fa fa-pencil-square-o" data-toggle="modal" data-target="#exampleModal1" data-offer_id="<%= emp['offer_letter_id'] %>" data-employee_id="<%= emp['employee_id'] %>"></button>               
</div>

modal "#exampleModal1"

    <div class="modal fade" id="exampleModal1" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
      </div>
      <div class="modal-body">
        <h2 class="text-center">Edit <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 => "update" } do |f| %>                
                    <div class="col-md-12">

                     <div class="mydata1">
                       <%= f.text_field :offer_letter_id, { class: 'form-control', id: 'offer_id-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>
                        <div class="mydata2">
                          <%= f.text_field :employee_id, { disabled: true, :required => true, placeholder: 'E12345678', class: 'form-control', id: 'employee_id-name' } %>
                        </div>
                        </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, { :required => true, 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, { :required => true,  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: 'datepicker1', 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'], { :required => true }, 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, { :required => true, placeholder: 'Father/Mother/Wife name', class: "form-control" } %>
                        </div>
                      </div> <br>

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

<script type="text/javascript">
    // edit employee information
    $('#exampleModal1').on('show.bs.modal', function (event) {
      var button = $(event.relatedTarget)
      var offer_id = button.data('offer_id')
      var employee_id = button.data('employee_id')
      var modal = $(this)
      modal.find('.mydata1 input').val(offer_id)
      modal.find('.mydata2 input').val(employee_id)
    });
</script>

<script type="text/javascript">
    // edit employee information
    $(document).ready(function () {        
        $('#datepicker1').datepicker({
            format: "dd/mm/yyyy"
        });    
    });
</script>

hr_controller.rb

class HrController < ApplicationController

    def new
        @employees = EmployeeDetail.new
    end

    # edit employee information
    def edit
        @employees = EmployeeDetail.find_by(params[:offer_letter_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_by(params[:offer_letter_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

routes.rb

  resources :hr
  get 'hr/edit'
  post 'hr/update'

There are some miss concept on you about edit and update method. you should find by id, i mean it should be like

@employees = EmployeeDetail.find(id)

and secondly please write a raise in update method to check really form passing the data or only passing the first data thats why its only updated your first column. in def update =>

raise params.inspect

then you will determine yourself params are correct or not, def employee_params method worked or not?

You should use

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

if offer_letter_id is your primary key or

def update
    @employees = EmployeeDetail.find_by(offer_letter_id: params[:employee_details][:offer_letter_id])

in any other case...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM