Shalaby
Shalaby

Reputation: 87

Rails pass generated js params from form to controller

I have a form_for with 2 date fields and 1 integer.

my js:

$(document).ready(function(){
  $("#start, #end").on('change', function () {
    var start = $('#start').datepicker('getDate');
    var end = $('#end').datepicker('getDate');
    var duration = (end.getDate() - start.getDate())/1000/60/60/24;

    $("#number_of_days").val(duration);
  });
});

the value of the variable "duration" shows up fine in the :number_of_days field in the form, but how do I go on about passing the generated :number_of_days to the controller when submitting the form?

Thank you

Edit: form_for:

<%= form_for(@vacation) do |f| %>  
  <div class="row">
    <div class="large-2 columns"><%= f.label :start, "Starts On" %></div>
    <div class="large-10 columns left"><%= f.text_field :start, data:{ type: 'date'}, id: 'start' %></div>
    </div>

    <div class="row">
      <div class="large-2 columns"><%= f.label :end, "Ends On" %></div>
      <div class="large-10 columns left"><%= f.text_field :end, id: 'end' %></div>
    </div>
    <div class="row">
      <div class="large-2 columns"><%= f.label :number_of_days, "Duration In Days" %></div>
      <div class="large-10 columns left"><%= f.text_field :number_of_days, disabled: true, id: 'number_of_days' %></div>
    </div>
    <div class="row">
      <div class="large-10 large-offset-2 columns">
      <%= f.submit nil, class: 'button small radius success' %>
      </div>
    </div>
 <% end %>

Controller action new and create:

def new
  @vacation = Vacation.new
end

def create
  @vacation = Vacation.new(vacation_params)
  @vacation.user = current_user
  @vacation.number_of_days = params[:number_of_days]
  respond_to do |format|
    if @vacation.save
      format.html { redirect_to @vacation, notice: 'Vacation was successfully created.' }
    else
      format.html { render action: 'new'}
    end
  end
end

permit params in private:

def vacation_params
  params.require(:vacation).permit(:start, :number_of_days, :end)
end

Upvotes: 2

Views: 1662

Answers (1)

Mandeep
Mandeep

Reputation: 9173

Just looked at your form and noticed this:

<div class="large-10 columns left"><%= f.text_field :number_of_days, disabled: true, id: 'number_of_days' %></div>

You have disabled: true for this field so when you submit your form its value doesn't get submitted. You need to update your js file this:

$(document).ready(function(){
  $("#start, #end").on('change', function () {
    var start = $('#start').datepicker('getDate');
    var end = $('#end').datepicker('getDate');
    var duration = (end.getDate() - start.getDate())/1000/60/60/24;

    $("#number_of_days").val(duration).attr("disable","false");
  });
});

OR

A better solution would be to make your field readonly rather than disabling it. You could do it by:

<div class="large-10 columns left"><%= f.text_field :number_of_days, readonly: true, id: 'number_of_days' %></div>

Upvotes: 1

Related Questions