我正在为我的应用程序使用ransack并试图创建一个可以使用sort_link排序的表头,但是当我尝试将sort_link应用于包含不是来自数据库表的值的表头时,我就陷入了困境,但是从模型中的方法......
这是我的代码
我的模特
def final_price_with_items
self.item_cost + self.final_price
end
我的模型中有一个白名单,用于声明包含数据库表中值的另一个表头。
self.whitelisted_ransackable_attributes = ['number','state', 'cost', 'created_at']
我的HTML
<table class="index" id="listing_shipments" data-hook>
<thead>
<tr data-hook="admin_shipments_index_headers">
<th><%= sort_link @search, :created_at, Spree::Shipment.human_attribute_name(:created_at) %></th>
<th><%= sort_link @search, :number, Spree::Shipment.human_attribute_name(:number) %></th>
<th><%= sort_link @search, :state, Spree::Shipment.human_attribute_name(:state) %></th>
<th><%= sort_link @search, :cost, Spree::Shipment.human_attribute_name(:cost) %></th>
<th><%= sort_link @search, :final_price_with_items, Spree::Shipment.human_attribute_name(:final_price) %></th>
<th data-hook="admin_shipments_index_header_actions" class="actions"></th>
</tr>
</thead>
<tbody>
<% @shipments.each do |shipment|%>
<tr id="<%= spree_dom_id shipment %>" data-hook="admin_shipments_index_rows" class="<%= cycle('odd', 'even')%>">
<td><%= l shipment.created_at.to_date %></td>
<td><%= link_to shipment.number, edit_admin_order_path(shipment.order) %></td>
<td><span class="state <%= shipment.state.downcase %>"><%= Spree.t("shipment_state.#{shipment.state.downcase}") %></span></td>
<td><%= shipment.display_cost.to_html %></td>
<td><%= Spree::Money.new(shipment.final_price_with_items, currency: shipment.currency).to_html %></td>
<td data-hook="admin_shipments_index_row_actions" class="actions align-center">
<%= link_to_edit_url edit_admin_order_path(shipment.order), :title => "admin_edit_#{dom_id(shipment)}", :no_text => true %>
</td>
</tr>
<% end %>
</tbody>
</table>
<*>在白名单中声明的表头能够排序,因为数据库中的出货表有这些列,但是final_price_with_items不能排序..任何人都可以帮我解决这个问题吗?
答案 0 :(得分:0)
应该能够与搜索者一起做。我可能会将您的排序链接名称更改为final_price_with_items_sort
然后在你的模型中添加:
ransacker :final_price_with_items_sort do
Arel.sql('item_cost + final_price')
end