Ruby on rails日期选择器没有添加到数据库。我能改变什么?

时间:2017-04-11 14:59:46

标签: jquery ruby-on-rails datepicker jquery-ui-datepicker bootstrap-datepicker

我有一个包含表单的结帐页面,我需要添加3个选项以通过日期选择器进行投放。 我使用了我正在复制的网站上的代码,并且我已经能够在页面上使用该表单,但是,在提交订单时,日期不会添加到数据库中。

我留下了一些已注释掉的代码,因为这些功能将在稍后添加。

 <script src="https://pi-test.sagepay.com/api/v1/js/sagepay-dropin.js"></script>
<div class="container">
  <div class="col-md-10 col-md-offset-1">
    <div class="col-md-12">
      <h3> Please Enter Your Details</h3>
    </div>
    <div class="col-md-12">
      <%= simple_form_for @order do |f| %>
        <%= f.input :name %>
        <%= f.input :email %>
        <%= f.input :address %>
        <%= f.input :city %>
        <%= f.input :postcode %>

        <h2>Payment Details</h2>
        <div id="sp-container"></div>
        <div class="form-actions">

          <div id="DeliveryRequirement"><div class="panel-body padding0 basketHeader font18">
    <div class="col-xs-12 col-sm-4 padding0">Delivery Requirements</div>
</div>

<div class="panel-body padding0 paddingBottom20 bold deliveryDateTime">

    <p>To continue to the Checkout you will need to agree to all 3 of the Delivery Requirements. Normally we have no issues at all, you can add any descriptions below that might help the driver. If you are unsure about any of the requirements please call 020-8819-6482 and we can take a look for you.</p>
    <div class="paddingBottom10">
        <a href="" onclick="$('#loginModal').modal('toggle');" data-toggle="modal" data-target="#deliveryModal">
            <img src="/assets/dAccess.png"></a>
    </div>
    <div class="padding0 paddingTop10 paddingBottom10"><input data-val="true" data-val-required="The DeliveryConfirm field is required." id="DeliveryDates_DeliveryConfirm" name="DeliveryDates.DeliveryConfirm" type="checkbox" value="true"><input name="DeliveryDates.DeliveryConfirm" type="hidden" value="false">I confirm that there is good access to the location with ample space for the driver to deliver the pallet safely.</div>
    <div class="padding0 paddingBottom10"><input data-val="true" data-val-required="The DeliveryDisagree field is required." id="DeliveryDates_DeliveryDisagree" name="DeliveryDates.DeliveryDisagree" type="checkbox" value="true"><input name="DeliveryDates.DeliveryDisagree" type="hidden" value="false">Please call me to discuss my delivery as there may be some issues with access.<br><br>Please enter any special requirements you may have when we make the delivery.</div>
    <div class="padding0 paddingBottom10"><textarea class="deliveryTextArea" cols="20" id="DeliveryDates_DeliveryDisagreeMessage" name="DeliveryDates.DeliveryDisagreeMessage" rows="5"></textarea><grammarly-btn><div data-reactroot="" class="_e725ae-textarea_btn _e725ae-not_focused" style="visibility: hidden; z-index: 2;"><div class="_e725ae-transform_wrap"><div title="Protected by Grammarly" class="_e725ae-status">&nbsp;</div></div></div></grammarly-btn>

       <span style="color:red"><span class="field-validation-valid" data-valmsg-for="DeliveryDates.DeliveryConfirm" data-valmsg-replace="true"></span></span>
        <span class="field-validation-valid" data-valmsg-for="DeliveryDates.DeliveryDisagreeMessage" data-valmsg-replace="true"></span>
    </div>
</div>

<div class="panel-body padding0 basketHeader font18 margin0">
    <div class="col-xs-12 col-sm-12 padding0">Please choose 3 different delivery dates (click here to see our delivery schedule), if you would like your delivery sooner, you can add the days and times for us to see if we can deliver any quicker.</div>
</div>

<div class="table-responsive checkouttable deliveryTable">
    <table class="table">
        <thead>
            <tr>
                <th>
                    <div class="oldPress1">FIRST CHOICE</div>
                    <div class="paddingTop10">Next available date</div>
                    <div class="padding0">
                        <input class="text" data-val="true" data-val-date="The field First choice must be a date." data-val-required="First choice required" id="ddate1" name="ddate1" type="text" value=""><img class="ui-datepicker-trigger" src="../assets/calendar.png" alt="Select date" title="Select date">
                    </div>
                    <span class="field-validation-valid" data-valmsg-for="DeliveryDates.DeliveryDateFirst" data-valmsg-replace="true"></span>
                </th>
                <script>
    $('#order_ddate1').ready(function(){
      var date_input=$('input[name="ddate1"]'); //our date input has the name "date"
      var container=$('.bootstrap-iso form').length>0 ? $('.bootstrap-iso form').parent() : "body";
      var options={
        format: 'dd/mm/yyyy',
        startDate: "+2d",
        container: container,
        todayHighlight: true,
        autoclose: true,
      };
      date_input.datepicker(options);
    })
</script>
                <th>

                    <div class="oldPress1">SECOND CHOICE</div>
                    <div class="paddingTop10">Next available date</div>
                    <div class="padding0">
                     <input class="datePickerInput marginTop10 hasDatepicker" data-val="true" data-val-date="The field Second choice must be a date." data-val-required="Second choice required" id="ddate2" name="ddate2" type="text" value=""><img class="ui-datepicker-trigger" src="../assets/calendar.png" alt="Select date" title="Select date">
                    </div>
                    <span class="field-validation-valid" data-valmsg-for="DeliveryDates.DeliveryDateSecond" data-valmsg-replace="true"></span>
                </th>
                <script>
    $(document).ready(function(){
      var date_input=$('input[name="ddate2"]'); //our date input has the name "date"
      var container=$('.bootstrap-iso form').length>0 ? $('.bootstrap-iso form').parent() : "body";
      var options={
        format: 'dd/mm/yyyy',
        startDate: "+2d",
        container: container,
        todayHighlight: true,
        autoclose: true,
      };
      date_input.datepicker(options);
    })
</script>
                <th>

                    <div class="oldPress1">THIRD CHOICE</div>
                    <div class="paddingTop10">Next available date</div>
                    <div class="padding0">
                        <input class="datePickerInput marginTop10 hasDatepicker" data-val="true" data-val-date="The field Third choice must be a date." data-val-required="Third choice required" id="ddate3" name="ddate3" type="text" value=""><img class="ui-datepicker-trigger" src="../assets/calendar.png" alt="Select date" title="Select date">
                    </div>
                    <span class="field-validation-valid" data-valmsg-for="DeliveryDates.DeliveryDateThird" data-valmsg-replace="true"></span>
                </th>
                <script>
    $(document).ready(function(){
      var date_input=$('input[name="ddate3"]'); //our date input has the name "date"
      var container=$('.bootstrap-iso form').length>0 ? $('.bootstrap-iso form').parent() : "body";
      var options={
        format: 'dd/mm/yyyy',
        startDate: "+2d",
        container: container,
        todayHighlight: true,
        autoclose: true,
      };
      date_input.datepicker(options);
    })
</script>
            </tr>
        </thead>


    </table>
</div>
<div class="bottomTreeBorder padding0">
    <h1 class="divTitleText">ACCEPTED PAYMENT TYPES</h1>
</div>

<div class="panel-body padding0 paddingTop20">
    <div class="col-md-6 padding0 paddingTop10">
        <img src="/images/paymentTypes.png">
    </div>
    <div class="col-md-6 padding0 alignRight paddingTop10">
        <img src="/images/norton.png">
    </div>
</div>

<div class="paddingTop10">
    <a href="#" onclick="$(this).closest('form#checkoutaddress').submit()">
        <img src="/images/continue.png"></a>
</div>

    </div>









          <div class="row">
  <div class="col-md-3 col-md-offset-3">
          <%= f.button  :submit,"Pay #{ number_to_currency @basket.total_price, unit: "£"}" %>
        </div></div>
      <%end%>
    </div>
  </div>
</div>
<script>
  sagepayCheckout({ merchantSessionKey: "<%=session[:merchantSessionKey]%>" }).form();
</script>
<div class="container">
<div class="sagepay">
<%= image_tag'/assets/sagepay.jpg' %>
</div>
</div>

这是运行的服务器代码

SQL (4.6ms)  INSERT INTO `orders` (`name`, `email`, `address`, `city`, `postcode`, `created_at`, `updated_at`) VALUES ('liam ****', 'liam@liam.com', '7 liams close', 'Chertsey', 'kt91ha', '2017-04-11 14:41:24', '2017-04-11 14:41:24')

Orders控制器。

class OrdersController < ApplicationController
  include CurrentBasket
  before_action :set_basket, only: [:new, :create]
  before_action :set_order, only: [:show, :edit, :destroy]

  def index
      @orders = Order.all 
    end

    def new
        #@statuses = Status.all.map{ |c| [c.name, c,id] }
        if @basket.product_items.empty?
            redirect_to root_url, notice: 'Your Basket is Empty'
            return
        end
        response = SagaPay.get_session_key
        response_hash = JSON.parse(response.body)
        session[:expiry] = response_hash["expiry"]
        session[:merchantSessionKey] = response_hash["merchantSessionKey"]

        @order = Order.new


    end

    def create
        response = SagaPay.transcation_request(@basket,session[:merchantSessionKey],params["card-identifier"],params[:order][:name],params[:order][:address],params[:order][:city],params[:order][:postcode])
        @order = Order.new(order_params)
        @order.add_product_items_from_basket(@basket)
#       @order.status_id = params[:status_id]
        if response.code == "201"
            if @order.save
                Basket.destroy(session[:basket_id])
                session[:basket_id] = nil
                session[:merchantSessionKey] = nil
                session[:expiry] = nil
                response_hash = JSON.parse(response.body)
                # OrderNotifier.received(@order).deliver
                redirect_to root_url, notice: 'Thank You for Your Order!'
            else
              render :new, notice: 'Please check your form'
            end
        else
            p response_hash = JSON.parse(response.body)
            flash[:alert] = "Please check your card details.#{response_hash['statusDetail']}"
            render :new, notice: "Please check your card details.#{response_hash['statusDetail']}"
        end
    end

    def show
         @orders = Order.where(id: params[:id])
    end

    def destroy
        @order.destroy
        redirect_to root_url, notice: 'Order deleted'
    end

    def edit
        @statuses = Status.all.map{ |c| [c.name, c,id] }
    end

    def update
    @order.status_id = params[:status_id]

    if @order.update(order_params)
        redirect_to order_path(@order)
    else 
        render 'edit'
    end

end



    private

    def set_order
        @order = Order.find(params[:id])
    end

    def order_params
        params.require(:order).permit(:name, :email, :address, :city, :county, :postcode, :ddate1, :ddate2, :ddate3)
    end

end

我尝试过不同的输入类型

对不起,如果它有点乱,我正在尝试将Asp.net网站转换为ruby。 谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

您正在使用强参数白名单,只需要和许可 您order_params permit method

上列出的元素

目前,当您允许dddate3通过命令,即params [order [ddate3]]

时,您将获得params as params [ddate3]

现在您需要将日期设置为

var date_input=$('input[name="order[ddate3]"]')

同样。

试试这个。希望这有助于