【问题标题】:How can post parameters month and day in calendar date select tag?如何在日历日期选择标签中发布参数月份和日期?
【发布时间】:2014-08-22 22:32:24
【问题描述】:

我安装了 calendar_date_select gem,我试图在 calendar_date_select 标记中发布格式参数,只有月份和日期。

日历日期选择脚本完美运行,但是当我选择一个日期时,它会显示这种格式“年-月-日”,所以我想要做的是单击它只会显示 calendar_date_select_tag 中的月份和日期,这可能吗?或者也许是另一种方法来删除这样的日志年份?

这是日志:

Processing TestController#sending (for 127.0.0.1 at 2014-08-22 17:17:06) [POST]
Parameters: {"d1"=>"01-01", "d2"=>"31-01", "authenticity_token"=>"yeah=", "commit"=>"Search"}

我不想每次都删除年份以使其正常工作,因为删除年份并保留月份和日期时实际上工作正常。

这里是控制器

def sending
  @clients= Client.find_by_mysql(["select * from clients where date_format(happy_birthday,'%m-%d') BETWEEN ? AND ?",params[:d1],params[:d2] ])
end

这里是风景

<head>
 <% CalendarDateSelect.format=(:hyphen_ampm )%>
 <% calendar_date_select_style "silver" %>
 <% translation_calendar %> 
</head>

<% form_tag :controller=>"test",:action=>"sending" do %>
  From: <%= calendar_date_select_tag "d1",params[:d1] %>
  TO:   <%= calendar_date_select_tag "d2",params[:d2] %>
  <%= submit_tag "Search", :class=>"button" %>
<% end %>

这是截图

我尝试了这个,但我得到了未定义的方法 `strftime' for "01-01":String

<%= calendar_date_select_tag "d1",params[:d1].strftime("%m-%d")%>

请有人可以帮助我或建议?

提前致谢。

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 calendar


    【解决方案1】:

    我看到了你的问题,这很简单,试试这个:

    在您的控制器中:

    def sending
     ##### THIS IS IF YOU HAVE PROBLEMS WITH NIL PARAMS IN CALENDAR TAG
     if params[:d1].nil?
       @d1 = params[:d1]
     else
       if params[:d1].present?
         @d1 = Date.parse(params[:d1]).strftime("%m-%d")
       end
     end
    
     if params[:d2].nil?
       @d2 = params[:d2]
     else
       if params[:d2].present?
         @d2 = Date.parse(params[:d2]).strftime("%m-%d")
       end
     end
     ################
    
     @clients= Client.find(:all,:conditions=>['date_format(happy_birthday,"%m-%d") BETWEEN ? AND ?',@d1,@d2]  )
    end
    

    在你看来:

    <% form_tag :controller=>"test",:action=>"sending" do %>
     From: <%= calendar_date_select_tag "d1",@d1 %>
     TO:   <%= calendar_date_select_tag "d2",@d2 %>
     <%= submit_tag "Search", :class=>"button" %>
    <% end %>
    

    希望这些提示能有所帮助。

    【讨论】:

    • 我终于得到了正确答案...非常感谢+1先生
    猜你喜欢
    • 1970-01-01
    • 2021-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    相关资源
    最近更新 更多