简体   繁体   English

Rails将生成的js参数从表单传递给控制器

[英]Rails pass generated js params from form to controller

I have a form_for with 2 date fields and 1 integer. 我有一个带有2个日期字段和1个整数的form_for。

  • :start, id: "start" :start,id:“开始”
  • :end, id: "end" :end,id:“结束”
  • :number_of_days, id: "number_of_days" :number_of_days,id:“ number_of_days”

my js: 我的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? 变量“ duration”的值在表单的:number_of_days字段中显示正常 ,但是在提交表单时如何继续将生成的:number_of_days传递给控制器​​?

Thank you 谢谢

Edit: form_for: 编辑: 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

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. 您已禁用:此字段为true ,因此在提交表单时不会提交其值。 You need to update your js file this: 您需要这样更新您的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).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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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