Ajax-datatables-rails How to add buttons to ajax response
My problem is that I don't know how to add different buttons that stay in state using ajax-datatables-rails server side processing gem . For example, before trying to do side-to-side processing, I had it in my opinion.
<% if current_user.sales_manager? || current_user.admin? %>
<td>
<%= link_to t('.destroy', :default => t("helpers.links.destroy")),
bill_sale_path(bill_sale),
:method => :delete %>
</td>
<% end %>
and after the tutorial on the gem homepage, in bill_sale_datatable.rb
I have this.
def data
records.map do |record|
[
link_to('Show', bill_sale_path(record), class: 'btn-xs btn-link'),
record.customer.name,
record.seller.name,
l(record.date_sold, format: :short),
record.payment_type.name,
record.payed,
number_to_currency(record.total),
CONDITION AND BUTTON HERE
]
end
end
Then, how do I use server side processing to provide different buttons belonging to the if condition?
source to share
Until I used the gem, I used datatables with Rails and this is what I did:
Store the button as a variable in front of the array and then just call the variable in the array.
records.map do |record|
record_button = ''
record_button = button_to('A', button1_path) if condition1
record_button = button_to('B', button2_path) if condition2
record_button = button_to('C', button3_path) if condition3
record_button = button_to('D', button4_path) if condition4
[
link_to('Show', bill_sale_path(record), class: 'btn-xs btn-link'),
record.customer.name,
record.seller.name,
l(record.date_sold, format: :short),
record.payment_type.name,
record.payed,
number_to_currency(record.total),
record_button
]
end
You can also use a case statement if that suits your needs.
Make sure you add
def_delegator :@view, :button_to
To the beginning of the file:
https://github.com/jbox-web/ajax-datatables-rails#using-view-helpers
source to share