开发者

datepicker jquery ui and rails 3(.1)

Hi i am using开发者_如何学JAVA the date picker jquery ui in combination with rails 3.1. The date picker looks brilliant, only the date isn't stored in the database? Only sometimes...? So that's a difficult error.

This is my .js file:

$(function() { 
    $("#question_deadline").datepicker({ duration: 'fast', maxDate: '+2m', minDate: 'now', showOn: "button", buttonImage: "calendar.gif", buttonImageOnly: true });
    $("#question_deadline").datepicker("option", "showAnim", "drop");
    $("#question_deadline").datepicker("option", "dateFormat", "DD, d MM, yy"); 
});

In my controller there's just plain rails script:

def create    
    @question = Question.new(params[:question])


    if @question.save
      redirect_to questions_path, :notice => "Successfully created question."
    else
      setup_questions
      render :index
    end
  end

In views file _form.html.erb i use a text_field to display the date:

  <div class="field">
    <%= f.label :content, "Question" %><br />
    <%= f.text_field :content, :placeholder => "type your question here.." %>
    <%= f.text_field :deadline %><br /> 
  </div>

Are there people who have experience with datepiacker jquery ui and rails, the ryan bates episode, didn't solve it, i think that was written in rails 2.3?

Regards, Thijs


First, you need to show us the view where you have the datepicker element. If it's like this:

<input type="text" name="question_deadline" id="question_deadline" />

When you submit this form, the parameters you receive in your controller (in the method "create") is called question_deadline. So in that create method you should first write:

if params[:question_deadline] != ""
    params[:question][:question_deadline] = params[:question_deadline]
end
#add a else if this date field is compulsory in the database

This step is important because the create method will read stuff from params[:question][:question_deadline] not from params[:question_deadline] which is returned from the view. Thus params[:question][:question_deadline] is empty when you do @question.save

To display the date, you also need to show us the controller "show" method that should be something like:

@question = Question.find(params[:id]) #or any sql request that returns info about a question.

Then in the view you can retrieve it simply with:

<%= @question.question_deadline%>

Maybe with more code from you controller and view I can elaborate on that.


I think, Rails/Ruby is not able to parse a date in this format:

$("#question_deadline").datepicker("option", "dateFormat", "DD, d MM, yy");
// full day name, day (w/o leading zero), full month name, 4-digit year

In your controller, you might want to add a line such as

def create/update
  ...
  @question.deadline = DateTime.strptime(params[:question][:deadline], '%A, %d %B, %Y')
  # assuming my jquery-to-ruby format-mapping is adequate ;-)
  if @question.save
    ...
end

Beware, that this code easily breaks on malformed date strings.

If you don't want to change the format to, e.g. 'yy-mm-dd' (in Ruby-land it's '%Y-%m-%d'), you may want to populate the selected date to another HTML element using the altField option and hide the actual datepicker input field via CSS:

$("#somewhere_else").datepicker(
  dateFormat: "%yy-%mm-%dd",
  altField:   "#question_deadline",
  altFormat:  "DD, d MM, yy",
  ...
);
<%= form_for @question do |f| %>
  ...
  <%= text_field_tag 'somewhere_else', @question.deadline %>
  <%= f.hidden_field :deadline %>
  ...
<% end %>

That'll work, at least for me :-)

—Dominik


The other option is to update the way ActiveSupport parses dates. This is outlined in Default Date Format in Rails (Need it to be ddmmyyyy)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜