Dar*_*use 2 ruby ruby-on-rails date type-conversion ruby-on-rails-4
我创建了一种方法,根据用户的生日计算用户的年龄.
在Rails服务器中运行此消息时收到无效的日期消息.
错误信息
ArgumentError in Users#show
Extracted source (around line #23):
20 </li>
21
22 <li>
23 <strong>Age:</strong> <%= "#{user_age}" %>
24 </li>
25
26 <li>
Run Code Online (Sandbox Code Playgroud)
方法
def user_age
Time.now.year - (@user.birthday.split('-').rotate(-1).join('-').to_date.year)
end
Run Code Online (Sandbox Code Playgroud)
在上面的方法中,我将用户的生日属性从字符串转换为日期.
我在Rails控制台中对此进行了测试,并且它运行良好,但它在Rails服务器中不起作用.
编辑页面
<% @title = "Edit Profile" %>
<h2>Update your information here</h2>
<div class = "center">
<%= form_for @user do |f| %>
<p>
<%= f.label :birthday, class: 'marker' %>
<%= f.text_field :birthday %>
</p>
<p>
<input class="btn btn-primary" type="submit" value="Update">
</p>
<% end %>
</div>
Run Code Online (Sandbox Code Playgroud)
用户控制器(重要操作)
class UsersController < ApplicationController
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update_attributes(user_params)
redirect_to @user
flash[:success] = "Your profile has been updated"
else
render 'edit'
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation, :location, :birthday)
end
end
Run Code Online (Sandbox Code Playgroud)
User.rb(重要)
class User < ActiveRecord::Base
VALID_DATE_REGEX = /\A(([1-9]|1[012])[-\/.]([1-9]|[12][0-9]|3[01])[-\/.](19|20)\d\d)|((1[012]|0[1-9])(3[01]|2\d|1\d|0[1-9])(19|20)\d\d)|((1[012]|0[1-9])[-\/](3[01]|2\d|1\d|0[1-9])[-\/](19|20)\d\d)\z/
validates :birthday, format: { with: VALID_DATE_REGEX }, :on => :update
private
def date_conversion
self.birthday = self.birthday.gsub('/', '-')
end
end
Run Code Online (Sandbox Code Playgroud)
当我在Rails控制台中运行它时,我的user_age方法有效,但是当我加载应用程序时收到无效的日期消息.
一个可能的问题是我最新的迁移文件,我在其中设置:age到int.
迁移文件
class AddAgeToUsers < ActiveRecord::Migration
def change
add_column :users, :age, :integer
add_column :users, :birthday, :string
end
end
Run Code Online (Sandbox Code Playgroud)
我仍然不明白为什么收到无效的日期消息.在我的方法中,我清楚地将:生日字段转换为日期.
这就是它在Rails Console中运行的原因.我不知道为什么它在Rails服务器中不起作用.
有任何想法吗?非常感谢帮助.
全堆跟踪(第一对线)
activemodel (4.0.4) lib/active_model/attribute_methods.rb:439:in `method_missing'
activerecord (4.0.4) lib/active_record/attribute_methods.rb:167:in `method_missing'
app/views/users/show.html.erb:23:in `_app_views_users_show_html_erb___1002307970_28886880'
actionpack (4.0.4) lib/action_view/template.rb:143:in `block in render'
activesupport (4.0.4) lib/active_support/notifications.rb:161:in `instrument'
actionpack (4.0.4) lib/action_view/template.rb:141:in `render'
actionpack (4.0.4) lib/action_view/renderer/template_renderer.rb:49:in `block (2 levels) in render_template'
actionpack (4.0.4) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
Run Code Online (Sandbox Code Playgroud)
事实证明这部分是罪魁祸首
def user_age
Time.now.year - (@user.birthday.split('-').rotate(-1).join('-').to_date.year)
end
Run Code Online (Sandbox Code Playgroud)
生日被记录为
12/13/1991
Run Code Online (Sandbox Code Playgroud)
我将字符串拆分为(' - ')字符中的数组,这在原始字符串中不存在.这就是日期无效的原因.
将生日改为
12-13-1991
Run Code Online (Sandbox Code Playgroud)
解决了这个问题.