【发布时间】:2015-09-15 09:06:57
【问题描述】:
这适用于使用 Sinatra 和 ActiveRecord 的应用程序。我试图在我的视图文件 (_form.erb) 上显示一条错误消息,但 Sinatra 只显示“我不知道这个小曲”消息,并没有在页面上显示实际的错误消息。
这适用于用户应该输入名字、姓氏和出生日期的表单。 "validates_presence_of :birthdate, :first_name, :last_name" 应该检查这些字段是否为空白,但我不确定我拥有的代码是否确实如此,这就是错误消息未显示的原因。特别是下面的帖子路线部分:
if params[:birthdate].include?("-")
birthdate = params[:birthdate]
else
birthdate = Date.strptime(params[:birthdate], "%m%d%Y")
end
假设总是有输入,因此如果您将生日字段留空并且永远不会显示错误消息,Sinatra 会说“ArgumentError:无效日期”。我不确定如何让 Sinatra 停止为生日字段提供该错误并在页面上显示错误消息,但如果我输入生日并将姓名字段留空,则不会显示错误消息。
这是我的 people_controller.rb 文件中的相关路由:
#create a new person
#if-else clause accounts for date input with dashes and without dashes (string input)
post "/people" do
if params[:birthdate].include?("-")
birthdate = params[:birthdate]
else
birthdate = Date.strptime(params[:birthdate], "%m%d%Y")
end
@person = Person.new(first_name: params[:first_name], last_name: params[:last_name], birthdate: birthdate)
if @person.valid?
@person.save
redirect "/people/#{@person.id}"
else
@error = "The data you entered isn't valid"
erb :"/people/new"
end
end
这是我的 people/new.erb 文件:
<h1>Create a Person</h1>
<%= erb :"/people/_form" %>
这是我的人/_form.erb 文件:
<% if !@errors.blank? %>
<p class="errors">
<%= "#{@errors}" %>
</p>
<% end %>
<form action="<%= people_form_action(@person) %>" method="post" id="<%= people_form_id(@person) %>" class="<%= people_form_class(@person) %>">
<input type="hidden" name="_method" value="<%= people_form_method(@person) %>" />
<div>
<label for="first_name">First Name:</label>
<input type="text" name="first_name" value="<%= @person.first_name %>" />
</div>
<div>
<label for="last_name">Last Name:</label>
<input type="text" name="last_name" value="<%= @person.last_name %>" />
</div>
<div>
<label for="birthdate">Birthdate:</label>
<input name="birthdate" type="date" value="<%= @person.birthdate %>" />
</div>
<div>
<input type="submit" />
</div>
</form>
这是我的 Person 类:
class Person < ActiveRecord::Base
validates_presence_of :birthdate, :first_name, :last_name
def self.get_birth_path_num(birthdate)
number = birthdate[0].to_i + birthdate[1].to_i + birthdate[2].to_i + birthdate[3].to_i + birthdate[4].to_i + birthdate[5].to_i + birthdate[6].to_i + birthdate[7].to_i
number = number.to_s
number = number[0].to_i + number[1].to_i
if(number > 9)
number = number.to_s
number = number[0].to_i + number[1].to_i
end
return number
end
def self.get_message(number)
case(number)
when(1)
message = "One is the leader. The number one indicates the ability to stand alone, and is a strong vibration. Ruled by the Sun."
when(2)
message = "This is the mediator and peace-lover. The number two indicates the desire for harmony. It is a gentle, considerate, and sensitive vibration. Ruled by the Moon."
when(3)
message = "Number Three is a sociable, friendly, and outgoing vibration. Kind, positive, and optimistic, Three's enjoy life and have a good sense of humor. Ruled by Jupiter."
when(4)
message = "Your numerology number is 4. This is the worker. Practical, with a love of detail, Fours are trustworthy, hard-working, and helpful. Ruled by Uranus."
when(5)
message = "This is the freedom lover. The number five is an intellectual vibration. These are 'idea' people with a love of variety and the ability to adapt to most situations. Ruled by Mercury."
when(6)
message = "This is the peace lover. The number six is a loving, stable, and harmonious vibration. Ruled by Venus."
when(7)
message = "This is the deep thinker. The number seven is a spiritual vibration. These people are not very attached to material things, are introspective, and generally quiet. Ruled by Neptune."
when(8)
message = "This is the manager. Number Eight is a strong, successful, and material vibration. Ruled by Saturn."
when(9)
message = "This is the teacher. Number Nine is a tolerant, somewhat impractical, and sympathetic vibration. Ruled by Mars."
end
end
#method to check if a user enters a valid birthdate
def self.valid_birthdate(input)
if(input.length==8 && input.match(/^[0-9]+[0-9]$/))
return true
else
return false
end
end
end
【问题讨论】:
标签: ruby validation activerecord sinatra