7

There's not a lot of documentation as to how to use the display_with option in Best In place, but I'm trying to get Best_in_Place to display dates in condensed form, (mm/dd/yyyy). My db (sqlserver) has the dates stored in datetime format, and I use this command to display the field:

<%= best_in_place(@production, :budget_approval_internal,  type: :date, :nil => "[Not    set]")  %>

When clicked the gem works as expected, putting up a calendar control to select the date, and then displaying it in short form. But when I press refresh I get a date that looks like:

 2013-12-04 00:00:00 UTC    

So I thought I could use the :display_with option to have use a helper that looks like this:

def format_date(my_date)
   my_date.strftime('%m/%d/%Y')
end

I put this in the application_helper.rb module and then tried this:

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

but I get an error in saying:

"can't find helper format_date. Any ideas?

GGizmos
  • 3,443
  • 4
  • 27
  • 72

4 Answers4

5

I ran into the same problem and used a small snippet from the pull requests of best_in_place done by zubin.

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

here the ldt is the method which takes a date object and converts to format of my desire.

Hope it helps

Zahiduzzaman
  • 197
  • 1
  • 3
0

Either you should include ApplicationHelper in your controller or add the method to the appropriate helper file.
Edit : You need to open the ActionView::Base class and add your method

module ActionView  
    class Base  
        def format_date(rec)  
            rec.strftime('%m/%d/%Y')  
        end  
    end  
end  
Sebi
  • 669
  • 5
  • 6
  • Nope, I believe ApplicationHelper is included automatically in all controllers, but in any event it didn't help. interestingly, if I just put <%= format_date(@production.budget_approval_internal) %> in the view, it works fine. I'm not sure how format_date is supposed to get the parameter from :display_with, but I'm assuming that's part of the best_in_place magic. – GGizmos Nov 26 '12 at 10:14
  • It looks like it only accepts helpers from the ActionView module. In lib/best_in_place/helper.rb there's a check `ViewHelpers.respond_to?(opts[:display_with])`. And the ViewHelpers is created like this `BestInPlace::ViewHelpers = ActionView::Base.new` – Sebi Nov 26 '12 at 10:51
  • So is there a way to mix in this function into the ActionView module somehow? – GGizmos Nov 26 '12 at 17:39
  • Good idea. Try adding this after the ApplicationHelper module : `module ActionView class Base def format_date(rec) rec.strftime('%m/%d/%Y') end end end ` – Sebi Nov 27 '12 at 09:04
0

Maybe you can override the setter method for budget_approval_internal of your production model. This is not the best solution but if you are using best_in_place in this model only then it ok.

class Production
  def budget_approval_internal
    read_attribute(:budget_approval_internal).strftime("dd/mm/YY")
  end
end
Super Engineer
  • 1,966
  • 1
  • 13
  • 21
  • I thought of that but it doesn't really addres the problem as to why I can't :display_with to work. I've got about 30 date fields in the model I'd have to deal with, and it seems like a lot of unnecessary code. – GGizmos Nov 26 '12 at 17:40
0

I just ran into a similar problem myself. For a simple formatting method like you have, you should just use :display_as instead of :display_with.

Your b_i_p code in the view would use :display_as => format_date.

Then put the format_date method in your model. Remove the argument and rewrite the method as follows:

def format_date
   self.my_date.strftime('%m/%d/%Y')
end

No need to mess around with ApplicationHelper.

Joel White
  • 33
  • 5