2016-04-04 7 views
0

私のアプリケーションにransackを使用していて、sort_linkでソート可能なテーブルヘッドを作成しようとしていますが、sort_linkをsort_linkをテーブルヘッドに適用しようとすると固くなります。ないデータベースのテーブルから、しかし、モデル内のメソッドから値が...ransack sort_linkモデルの定義メソッドに基づいて

これは私のコード

私のモデル

def final_price_with_items 
    self.item_cost + self.final_price 
end 

であると私は宣言するために私のモデルにホワイトリストに登録していましたテーブルの値を含むもう1つのテーブルヘッドデータベース。

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

ransackerで行うことができます。私は多分次に、あなたのモデルでこれを追加しfinal_price_with_items_sort

にあなたのソート・リンクの名前を変更したい:

ransacker :final_price_with_items_sort do Arel.sql('item_cost + final_price') end

関連する問題