表单中的更改不会保存到数据库 [英] Changes in the form are not saved to database

查看:139
本文介绍了表单中的更改不会保存到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我添加了bootstrap-datepicker-rails gem,所有需要的CSS和javascript。



当我在日历中选择日期并单击保存时,它不会在数据库中更新。



以下是表单的代码:

  = simple_form_for @model 
= f.input:date_last,:required => true,
:input_html => {data:{behavior:datepicker}},
:as => :string
= f.button:submit,'Save',:class => 'btn btn-primary'

我认为这可能与格式有关,但不知道在哪里



更新



当我将默认格式更改为yyyy时, -mm-dd(默认为mm / dd / yyyy):

  $('[data-behavior〜= datepicker] ).datepicker({format:yyyy-mm-dd,weekStart:1,autoclose:true}); 


解决方案

我刚刚回答了一个类似的问题这里,并在下面列出了我的答案:






我还需要得到这个工作并支持不使用以美国为中心的日期格式的多个区域设置,例如:en-AU(澳大利亚),日期格式为dd / mm / yyyy。



问题



Rails期望日期格式为 yyyy-mm-dd 浏览器,但您希望在用户的区域设置中显示日期。虽然日期控件允许您指定显示格式,但不允许您单独指定要发送回服务器的格式。



解决方案



构建一个隐藏的输入字段,将正确的格式发送回您的rails服务器。我使用自定义simple_form输入控件来构建日期选择器输入字段和隐藏字段,并在输入控件更改时使用一些JavaScript转换为rails日期。



结果是,您可以在rails中使用一个很好的引导日期选择器,并使用simple_form,如下所示:

 <%= f.input:date,as::bootstrap_datepicker%> 

或使用长日期格式:

 <%= f.input:date,as::bootstrap_datepicker,input_html:{format::long}%> 



实施



创建或编辑以下内容文件:



Gemfile

  gem 'bootstrap-sass'
gem'bootstrap-datepicker-rails'

配置/locales/en-AU.yml

  en-AU:
日期:
datepicker:
默认值:dd / mm / yyyy
long:dd MM,yyyy
格式:
默认值:! '%d /%m /%Y'
long:! '%d%B,%Y'

config / locales / en-US.yml

  en-US:
date:
datepicker:
default: mm / dd / yyyy
long:MM dd,yyyy
格式:
默认值:%m /%d /%Y
long: '%B%d,%Y'

app / assets / stylesheets / application.css .scss

  @importbootstrap-responsive; 
@importbootstrap-datepicker;

app / assets / javascripts / application.js.coffee

 #=需要bootstrap 
#=需要bootstrap-datepicker
#=需要bootstrap-datepicker-rails

或者, app / assets / javascripts / application.js

  // = require bootstrap 
// = require bootstrap-datepicker
// = require bootstrap-datepicker-rails

app / assets / javascripts / bootstrap-datepicker-rails.js.coffee

  $  - > 
#将bootstrap-datepicker值转换为rails日期格式(yyyy-mm-dd)在我们的隐藏字段
$(document).on'changeDate','.bootstrap-datepicker',(evt) >
rails_date = evt.date.getFullYear()+' - '+('0'+(evt.date.getMonth()+ 1))slice(-2)+' - '+('0' + evt.date.getDate())。slice(-2)
$(this).next(input [type = hidden])。val(rails_date)

应用/输入/ bootstrap_datepicker_input.rb

  class BootstrapDatepickerInput< SimpleForm :: Inputs :: Base 
def input
text_field_options = input_html_options.with_indifferent_access
format = text_field_options.delete(:format)
hidden_​​field_options = text_field_options.dup
hidden_​​field_options [ :class] = text_field_options [:class] .dup#,因此它们不能与同一个数组对象一起使用
hidden_​​field_options [:id] =#{attribute_name} _hidden
text_field_options [:class] < 'bootstrap-datepicker'
text_field_options [:type] ='text'
text_field_options [:value] || = format_date(value(object),format)
set_data_option text_field_options,'date-format ',I18n.t(format,scope:[:date,:datepicker],default::default)
default_data_option text_field_options,'provide','datepicker'

return_string =
#{@ builder.text_field(attribute_name,text_field_options.to_hash)} \\\
+
#{@ builder.hidden_​​field(attribute_name,hidden_​​field_options.to_hash)} \\\

return return_string .html_safe
end

protected

def default_data_option(hash,key,value)
set_data_option(hash,key,value)除非data_option(hash, key)
end

def data_option(hash,key)
hash [:data] .try(:[],key)|| hash [data - #{key}]
end

def set_data_option(hash,key,value)
哈希[:data] .try(:[] =,关键,价值)|| (hash [data - #{key}] = value)
end

def value(object)
object.send @attribute_name if object
end

def format_date(value,format = nil)
value.try(:strftime,I18n.t(format,scope:[:date,:formats],default::default))
end
end


I'm trying to add a date field to my form.

I added bootstrap-datepicker-rails gem, all required css and javascript.

When I choose the date in the calendar and click Save, it's not updated in the database.

Here's the code of the form:

= simple_form_for @model
  = f.input :date_last, :required => true, 
            :input_html => { data: {behaviour: "datepicker"}}, 
            :as => :string
  = f.button :submit, 'Save', :class => 'btn btn-primary'

I think this might have to do with formats, but not sure where to look at.

Update

It works when I change default format to yyyy-mm-dd (default is mm/dd/yyyy):

$('[data-behaviour~=datepicker]').datepicker({"format": "yyyy-mm-dd", "weekStart": 1, "autoclose": true});

解决方案

I just answered a similar question here and have included my answer below:


I also needed to get this working and support multiple locales that do not use US centric date formats, eg: en-AU (Australia) which has the date format dd/mm/yyyy.

The Problem

Rails expects the date format to be yyyy-mm-dd from the browser, yet you want to display the date in the user's locale. Whilst the date control allows you to specify the format for display it does NOT allow you to separately specify the format to be sent back to the server.

The Solution

Build a hidden input field that sends the correct format back to your rails server. I do this with with a custom simple_form input control that builds both the date-picker input field and a hidden field, and use some javascript to convert to the rails date when the input control changes.

The upshot is you can have a nice bootstrap date-picker in rails and use it with simple_form as follows:

<%= f.input :date, as: :bootstrap_datepicker %>

Or to use a long date format:

<%= f.input :date, as: :bootstrap_datepicker, input_html: { format: :long } %>

Implementation

Create or edit the following files:

Gemfile

gem 'bootstrap-sass'
gem 'bootstrap-datepicker-rails'

config/locales/en-AU.yml

en-AU:
  date:
    datepicker:
      default: "dd/mm/yyyy"
      long: "dd MM, yyyy"
    formats:
      default: ! '%d/%m/%Y'
      long: ! '%d %B, %Y'

config/locales/en-US.yml

en-US:
  date:
    datepicker:
      default: "mm/dd/yyyy"
      long: "MM dd, yyyy"
    formats:
      default: "%m/%d/%Y"
      long: ! '%B %d, %Y'

app/assets/stylesheets/application.css.scss

@import "bootstrap-responsive";
@import "bootstrap-datepicker";

app/assets/javascripts/application.js.coffee

#= require bootstrap
#= require bootstrap-datepicker
#= require bootstrap-datepicker-rails

Alternatively, app/assets/javascripts/application.js

//= require bootstrap
//= require bootstrap-datepicker
//= require bootstrap-datepicker-rails

app/assets/javascripts/bootstrap-datepicker-rails.js.coffee

$ ->
  # convert bootstrap-datepicker value to rails date format (yyyy-mm-dd) on our hidden field
  $(document).on 'changeDate', '.bootstrap-datepicker', (evt) ->
    rails_date = evt.date.getFullYear() + '-' + ('0' + (evt.date.getMonth() + 1)).slice(-2) + '-' + ('0' + evt.date.getDate()).slice(-2)
    $(this).next("input[type=hidden]").val(rails_date)

app/inputs/bootstrap_datepicker_input.rb

class BootstrapDatepickerInput < SimpleForm::Inputs::Base
  def input
    text_field_options = input_html_options.with_indifferent_access
    format =  text_field_options.delete(:format)
    hidden_field_options = text_field_options.dup
    hidden_field_options[:class] = text_field_options[:class].dup # so they won't work with same array object
    hidden_field_options[:id] = "#{attribute_name}_hidden"
    text_field_options[:class] << 'bootstrap-datepicker'
    text_field_options[:type] = 'text'
    text_field_options[:value] ||= format_date(value(object), format)
    set_data_option text_field_options, 'date-format', I18n.t(format, scope: [:date, :datepicker], default: :default)
    default_data_option text_field_options, 'provide', 'datepicker'

    return_string =
      "#{@builder.text_field(attribute_name, text_field_options.to_hash)}\n" +
      "#{@builder.hidden_field(attribute_name, hidden_field_options.to_hash)}\n"
    return return_string.html_safe
  end

protected

  def default_data_option(hash, key, value)
    set_data_option(hash,key,value) unless data_option(hash, key)
  end

  def data_option(hash, key)
    hash[:data].try(:[],key) || hash["data-#{key}"]
  end

  def set_data_option(hash, key, value)
    hash[:data].try(:[]=,key,value) || (hash["data-#{key}"] = value)
  end

  def value(object)
    object.send @attribute_name if object
  end

  def format_date(value, format=nil)
    value.try(:strftime, I18n.t(format, scope: [ :date, :formats ], default: :default))
  end
end

这篇关于表单中的更改不会保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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