[英]best_in_place when using :display_with shows text AND nil value. Only allows editing of nil value
[英]Best_in_place display_with not finding helper
關於如何在Best in place中使用display_with選項的文檔並不多,但我試圖讓Best_in_Place以濃縮形式顯示日期,(mm / dd / yyyy)。 我的db(sqlserver)具有以datetime格式存儲的日期,我使用此命令顯示該字段:
<%= best_in_place(@production, :budget_approval_internal, type: :date, :nil => "[Not set]") %>
單擊時,gem按預期工作,設置日歷控件以選擇日期,然后以簡短形式顯示。 但是當我按下刷新時,我得到的日期看起來像:
2013-12-04 00:00:00 UTC
所以我想我可以使用:display_with選項來使用看起來像這樣的幫助器:
def format_date(my_date)
my_date.strftime('%m/%d/%Y')
end
我把它放在application_helper.rb模塊中然后嘗試了這個:
<%= best_in_place(@production, :budget_approval_internal, type: :date, :display_with => :format_date, :nil => "[Not set]") %>
但我說錯了:
“找不到幫手format_date。有什么想法嗎?
我遇到了同樣的問題,並使用了來自zubin完成的best_in_place的pull請求的小片段。
<%= in_place_edit(@term, :starts_on, display_with: lambda { |dt| ldt(dt) }, html_attrs: {datepicker: "true"}) %>
這里的ldt是一個采用日期對象並轉換為我的願望格式的方法。
希望能幫助到你
您應該在控制器中include ApplicationHelper
,或者將方法添加到相應的幫助文件中。
編輯:您需要打開ActionView :: Base類並添加您的方法
module ActionView
class Base
def format_date(rec)
rec.strftime('%m/%d/%Y')
end
end
end
也許您可以覆蓋生產模型的budget_approval_internal
的setter方法。 這不是最佳解決方案,但如果您在此模型中使用best_in_place,那么就可以了。
class Production
def budget_approval_internal
read_attribute(:budget_approval_internal).strftime("dd/mm/YY")
end
end
我自己也遇到了類似的問題。 對於像您這樣的簡單格式化方法,您應該使用:display_as
而不是:display_with
。
您在視圖中的b_i_p代碼將使用:display_as => format_date
。
然后將format_date
方法放在模型中。 刪除參數並重寫方法如下:
def format_date
self.my_date.strftime('%m/%d/%Y')
end
無需使用ApplicationHelper。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.