lundi 9 mai 2016

Rails - How to get data from DB using Ajax GET method request

Rail4,jQuery Ajax

I need to display order for one day which chosen on - jQuery Datepicker.

I set up Ajax request and make some staff into my contoroller but I can't to make it works. Need advise.

Order_controller

def index
  @order = params[:date] ? Order.where(date: params[:date]) : Order.get_order_today
  respond_to do |format|
    format.html # render index.html.haml 
  end

end

order.js.coffee

$ ->
  $('#datepicker').datepicker dateFormat: 'yy-mm-dd', onSelect: (dateStr) ->
     $.ajax ->
        type: "GET",
        url: "orders/index",
        dataType: "html",
        data: {date: dateStr}
        success: ->
           # Should i do something here for render default index view?
           console.log("everything is ok ;)")

view/orders/index

     #order.col-xs-12.col-sm-6.col-md-8
        = render 'orders/order'
     #datepicker.col-xs-6.col-md-4.date-picker-div

view/orders/_order

%h3 Order info
%table.table.table-striped
  %tr
   %th Meal
   %th Price
 - if (!@order.nil?)
   - @order.products.each do |one_order|
     %tr
      %th=one_order.product_name
      %th=one_order.product_price
- else
     %tr
      %th no order yet
      %th
 - if (!@order.nil?)
    .col-md-8
       %span=link_to "Cancel order", delete_order_path(id: @order.id), method: :delete, :data => {:confirm => "You Sure?"},
                                          class: "btn btn-primary"
 - else
   .col-md-8
      %span=link_to "Create order", new_order_path , class: "btn btn-primary"
  .col-md-4.total-price-div
  %h4 Order coast: #{get_order_coast}

- console show me that when day was chosen on datepicker

Console:

enter image description here

Aucun commentaire:

Enregistrer un commentaire