Rails 3.1 text_field表单助手和jQuery datepicker日期格式 [英] Rails 3.1 text_field form helper and jQuery datepicker date format

查看:57
本文介绍了Rails 3.1 text_field表单助手和jQuery datepicker日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Rails视图中有一个表单字段,如下所示:

I have a form field in my Rails view like this:

<%= f.text_field :date, :class => "datepicker" %>

一个javascript函数将该类的所有输入字段转换为jQueryUI日期选择器.

A javascript function converts all input fields of that class to a jQueryUI datepicker.

$(".datepicker").datepicker({
                dateFormat : "dd MM yy",
                buttonImageOnly : true,
                buttonImage : "<%= asset_path('iconDatePicker.gif') %>",
                showOn : "both",
                changeMonth : true,
                changeYear : true,
                yearRange : "c-20:c+5"
            })

到目前为止,一切都很好.我可以编辑记录,并且它会将日期正确地一直保留到数据库中.我的问题是当我想再次编辑记录时.从记录中预填充表单后,它将以"yyyy-mm-dd"格式显示. javascript仅格式化在datepicker控件中选择的日期.有什么办法可以格式化从数据库中检索到的日期?我可以在哪个阶段进行此操作?

So far so good. I can edit the record and it persists the date correctly all the way to the DB. My problem is when I want to edit the record again. When the form is pre-populated from the record it displays in 'yyyy-mm-dd' format. The javascript only formats dates which are selected in the datepicker control. Is there any way I can format the date retrieved from the database? At which stage can I do this?

谢谢, 丹妮.

在下面的讨论之后,有更多细节,我的表单分布在两个文件中,一个是视图,另一个是局部.这是视图:

Some more details following the discussion below, my form is spread across two files, a view and a partial. Here's the view:

<%= form_tag("/shows/update_individual", :method => "put") do %>
    <% for show in @shows %>
        <%= fields_for "shows[]", show do |f| %>
            <%= render "fields", :f => f %>
        <% end %>
    <% end %>
    <%= submit_tag "Submit"%>
<% end %>

这是_fields部分视图:

<p>
    <%= f.label :name %>
    <%= f.text_field :name %>
</p>

<p>
    <%= f.date %>
    <%= f.label :date %>
    <%= f.text_field :date, :class => "datepicker" %>
</p>

控制器代码:

def edit_individual
    @shows = Show.find(params[:show_ids])
  end

我在environment.rb中添加了以下内容:

I have added the following in environment.rb:

Date::DATE_FORMATS.merge!(
  :default => "%d %B %Y"
)

现在,当我在视图中使用@ show.date时,它会显示正确的格式,但是表单助手仍在显示原始格式.

Now it's displaying the right format when I use @show.date in the view, but the form helper is still displaying the raw format.

推荐答案

已解决:我按照上述Jayendra的建议通过了:value => show.date.它不起作用的原因是因为我没有从父视图传递show对象.我更改了render行以包含它-父视图现在看起来像这样:

SOLVED: I pass in the :value => show.date as suggested by Jayendra above. The reason it wasn't working was because I didn't pass in the show object from the parent view. I changed the render line to include it - the parent view now looks like this:

<%= form_tag("/shows/update_individual", :method => "put") do %>
    <% for show in @shows %>
        <%= fields_for "shows[]", show do |f| %>
            <%= render "fields", :f => f, :show => show %>
        <% end %>
    <% end %>
    <%= submit_tag "Submit"%>
<% end %>

这篇关于Rails 3.1 text_field表单助手和jQuery datepicker日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆