如何使用日期时间精度宝石

时间:2015-01-23 07:32:16

标签: ruby-on-rails ruby date-format

我试图允许用户以非常宽松的格式输入日期(例如,仅年份,年份和月份或确切日期)。 date_time_precision宝石看起来很完美,但我是新手,我无法弄清楚(除了安装宝石)如何让我的表格能够达到各种精确度(I' ve)多次阅读自述文件。)

我在_form的日期部分使用了带有以下内容的simple_form gem:

<%= f.input :date, 
            :include_blank => true, 
            as: :string, 
            order: [ :year, :month, :day,], 
            hint: 'Order: Year, month, date' %>

文档的标题为&#39; Usage&#39;,然后是......

require 'date_time_precision'

但是我无法弄清楚把它扔到哪里。它会在我的控制器中某处吗?

更多信息:

我做了所有常见的事情(将gem添加到gemfile,bundle install,restart server和refresh form页面)。

更新

我的表单现在看起来已经破了,我不断收到类似ArtworksController#update中的&#34; ActionController :: ActionControllerError的错误 无法重定向到nil!&#34;

我的routes.rb文件

Rails.application.routes.draw do
  root "pages#home"

  get "artworks" => "artworks#index"
  # post "artworks" => "artworks#index"

  resources :artworks #, path: ''

  get "about" => "pages#about"
end

我的控制器:

    class ArtworksController < ApplicationController
  before_action :set_artwork, only: [:show, :edit, :update, :destroy]

  # GET /artworks
  # GET /artworks.json
  def index
    @artworks = Artwork.all
  end

  # GET /artworks/1
  # GET /artworks/1.json
  def show
    @artwork = Artwork.friendly.find(params[:id])
    if request.path != artwork_path(@artwork)
      redirect_to @artwork, status: :moved_permanently
    end
  end

  # GET /artworks/new
  def new
    @artwork = Artwork.new
  end

  # GET /artworks/1/edit
  def edit
  end

  # POST /artworks
  # POST /artworks.json
  def create
    @artwork = Artwork.new( artwork_params )

    respond_to do |format|
      if @artwork.save
        format.html { redirect_to @artwork, notice: 'Artwork was successfully created.' }
        format.json { render :show, status: :created, location: @artwork }
      else
        format.html { render :new }
        format.json { render json: @artwork.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /artworks/1
  # PATCH/PUT /artworks/1.json
  def update
    respond_to do |format|
      if @artwork.update(artwork_params)
        format.html { redirect_to @artworks, notice: 'Artwork was successfully updated.' }
        format.json { render :show, status: :ok, location: @artwork }
      else
        format.html { render :edit }
        format.json { render json: @artwork.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /artworks/1
  # DELETE /artworks/1.json
  def destroy
    @artwork.destroy
    respond_to do |format|
      format.html { redirect_to artworks_url, notice: 'Artwork was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_artwork
      @artwork = Artwork.friendly.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def artwork_params
      params.require(:artwork).permit(:title, :image, :genre, :category, :medium, :slug, :availability, :date, :height, :width)
    end
end

2 个答案:

答案 0 :(得分:0)

有时需要操纵已知不完整信息的日期或时间。例如,人们可能只知道年份,或年份和月份。遗憾的是,Ruby的内置日期,时间和日期时间类不会跟踪数据的精确度。 在上述方面,使用date_time_precision gem。只是提供你的帮助方法来完成日期/时间对象。

你的案子: 在您的视图中,您需要一个日期选择器/时间日期选择器(以获得更好的使用体验)。 我建议你去bootstrap-datepicker-rails gem

Instructions are clearly stated in the github link : 
 1. Install Gem. 
 2. Mark the related js files required in your application.js file.
 3. Add your datepicker / datetimepicker to a specific class (in document ready block)
 4. Use the datetimepicker in your View. 

答案 1 :(得分:0)

date_time_precision库将Ruby的DateTime类扩展为具有precision属性,该属性跟踪是否只知道年份,只知道年份和月份,或者年,月和日。

首先,install the library as a gem与此:

gem install date_time_precision

然后,此代码将为您工作:

require 'date_time_precision'
def Date.parse_fuzzy(str)
  if /\A\s*(\d{4})\s*\z/ =~ str
    Date.new($1.to_i)
  else
    Date.parse(str)
  end
end

那只是Date.parse的扩展,也可以接受独立年份。享受吧!