加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 如果Rails中的日期格式不正确,如何正确显示验

发布时间:2020-12-16 21:21:34 所属栏目:百科 来源:网络整理
导读:我正在使用Rails 4.2.7.如果用户没有以正确的格式输入他们的出生日期字段,我想抛出验证错误,所以我有 def update @user = current_user begin @user.dob = Date.strptime(params[:user][:dob],'%m/%d/%Y') rescue ArgumentError = ex end if @user.update_at
我正在使用Rails 4.2.7.如果用户没有以正确的格式输入他们的出生日期字段,我想抛出验证错误,所以我有
def update
    @user = current_user
    begin
      @user.dob = Date.strptime(params[:user][:dob],'%m/%d/%Y')
    rescue ArgumentError => ex
    end
    if @user.update_attributes(user_params)

在我看来,我有这个

<%= f.text_field :dob,:value => (f.object.dob.strftime('%m/%d/%Y') if f.object.dob),:size => "20",:class => 'textField',placeholder: 'MM/DD/YYYY' %>
      <% if @user.errors[:dob] %><%= @user.errors[:dob] %><% end %>

但是,即使有人输入“01-01 / 1985”之类的日期,上述内容也不会向视图返回验证错误.我需要做什么才能正确返回验证错误?

编辑:根据给出的答案之一,我试过了

@user = current_user
begin
  @user.dob = Date.strptime(params[:user][:dob],'%m/%d/%Y')
rescue ArgumentError => ex
  puts "Setting error." 
  @user.errors.add(:dob,'The birth date is not in the right format.')
end
if @user.update_attributes(user_params)
  last_page_visited = session[:last_page_visited]
  if !last_page_visited.nil?
    session.delete(:last_page_visited)
  else
    flash[:success] = "Profile updated"
  end
  redirect_to !last_page_visited.nil? ? last_page_visited : url_for(:controller => 'races',:action => 'index') and return
else
  render 'edit'
end

即使我可以看到调用的“救援”分支,我也没有被引导到我的“渲染’编辑’”块.

解决方法

调用update_attributes会清除您在急救中设置的错误.你应该检查错误,如果没有,那么继续,像这样:
@user = current_user
begin
  @user.dob = Date.strptime(params[:user][:dob],'The birth date is not in the right format.')
end
if !@user.errors.any? && @user.update_attributes(user_params)
  last_page_visited = session[:last_page_visited]
  if !last_page_visited.nil?
    session.delete(:last_page_visited)
  else
    flash[:success] = "Profile updated"
  end
  redirect_to !last_page_visited.nil? ? last_page_visited : url_for(:controller => 'races',:action => 'index') and return
end

render 'edit'

由于你redirect_to …并返回,你可以关闭条件,如果你做到这一点,只需渲染编辑页面.

您可能还想为用户模型添加简单验证:

validates :dob,presence: true

如果dob无法设置为其他一些不可预见的原因,这将始终失败.

要使用户输入的字符串在重新加载时填充该字段,您可以为用户模型添加一个访问者:dob_string

attr_accessor :dob_string

def dob_string
  dob.to_s
  @dob_string || dob.strftime('%m/%d/%Y')
end

def dob_string=(dob_s)
  @dob_string = dob_s
  date = Date.strptime(dob_s,'%m/%d/%Y')
  self.dob = date
rescue ArgumentError
  puts "DOB format error"
  errors.add(:dob,'The birth date is not in the correct format')
end

然后更改表单以设置:dob_string

<%= form_for @user do |f| %>
  <%= f.text_field :dob_string,:value => f.object.dob_string,placeholder: 'MM/DD/YYYY' %>
  <% if @user.errors[:dob] %><%= @user.errors[:dob] %><% end %>
  <%= f.submit %>
<% end %>

并更新控制器以设置dob_string:

def update
  @user = User.first
  begin
    #@user.dob = Date.strptime(params[:user][:dob],'%m/%d/%Y')
    @user.dob_string = user_params[:dob_string]
  end
  if ! @user.errors.any? && @user.update_attributes(user_params)
    redirect_to url_for(:controller => 'users',:action => 'show') and return
  end
  render 'edit'
end

def user_params
  params.require(:user).permit(:name,:dob_string)
end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读