Best_in_place display_with not find helper

use*_*110 7 ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2 best-in-place

关于如何在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]")  %>
Run Code Online (Sandbox Code Playgroud)

单击时,gem按预期工作,设置日历控件以选择日期,然后以简短形式显示.但是当我按下刷新时,我得到的日期看起来像:

 2013-12-04 00:00:00 UTC    
Run Code Online (Sandbox Code Playgroud)

所以我想我可以使用:display_with选项来使用看起来像这样的帮助器:

def format_date(my_date)
   my_date.strftime('%m/%d/%Y')
end
Run Code Online (Sandbox Code Playgroud)

我把它放在application_helper.rb模块中然后尝试了这个:

 <%= best_in_place(@production, :budget_approval_internal,  type: :date, :display_with => :format_date, :nil => "[Not    set]")  %>
Run Code Online (Sandbox Code Playgroud)

但我说错了:

"找不到帮手format_date.有什么想法吗?

小智 5

我遇到了同样的问题,并使用了zubin从best_in_place的拉取请求中提取的一小段代码。

<%= in_place_edit(@term, :starts_on, display_with: lambda { |dt| ldt(dt) }, html_attrs: {datepicker: "true"}) %>

在这里,ldt是一种使用日期对象并将其转换为我想要的格式的方法。

希望能帮助到你


Seb*_*ebi 0

您应该include ApplicationHelper在控制器中或将方法添加到适当的帮助程序文件中。
编辑:您需要打开 ActionView::Base 类并添加您的方法

module ActionView  
    class Base  
        def format_date(rec)  
            rec.strftime('%m/%d/%Y')  
        end  
    end  
end  
Run Code Online (Sandbox Code Playgroud)