当我将数据输入到我知道应该失败的表单中时,它不会。根据我下面的模型,我要求数据是 az、AZ、0-9 和空格的任意组合,并且当我在表单中填充垃圾时,例如 )( &^%^& (*&%^&** 或即使提交一个空字段,我希望会有错误。在这种情况下,没有。
这是模型的突出部分:
class Numerology
include ActiveModel::Model
attr_accessor :phrase
VALID_PHRASE_REGEX = /\A[a-zA-Z0-9\s]+\z/
validates :phrase, presence: true, length: { minimum: 1 },
format: { with: VALID_PHRASE_REGEX }
这是控制器......我想要它做的是返回索引(上面有表单的页面),当我在表单上提供错误输入时会产生任何错误。不确定我在这里是否以正确的方式进行,但我认为这可能是次要问题,因为我似乎根本没有产生任何错误(所以当然@numerology.errors.any? 会是错误的) .
class NumerologiesController < ApplicationController
before_filter :authenticate_user!
respond_to :html
def index
@numerology = Numerology.new
end
def create
@numerology = Numerology.new(params[:numerology])
if @numerology.errors.any?
render :index
else
@numresults = Numerology.analysis(params[:numerology][:phrase])
end
end
end
最后,这里是视图,首先是索引,然后是创建:
索引页面:
<div class="center jumbotron">
<h1>Numerology Analysis Module</h1>
<br>
<%= bootstrap_form_for(@numerology, layout: :horizontal, label_col: "col-sm-4", control_col: "col-sm-6") do |f| %>
<p> Enter word or phrase to be analyzed in the field below (Required).</p>
<%= @numerology.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
<%= f.text_field :phrase, label: "Word or Phrase: " %>
<br><br>
<%= f.submit "Perform Numerological Analysis" %>
<% end %>
<br><br>
<%= image_tag("SMILE.jpg", alt: "Smiley Face") %>
</div>
创建页面:
<div class="center jumbotron">
<h1>Numerological Analysis Report</h1>
<div class="numreport">
<table class="numtable">
<% @numresults.each do |line| %>
<% if !line.nil? %>
<tr>
<td><%= "#{line}" %></td>
</tr>
<% end %>
<% end -%>
</table>
</div>
<%= link_to "Perform Another Numerological Analysis - Click Here", numerologies_path %>
<br><br><br>
<%= image_tag("SMILE.jpg", alt: "Smiley Face") %>
</div>
那么,有人看到我在这里做错了吗?建议尝试其他事情?谢谢!