Ruby on Rails 的新手,在验证方面遇到了一些问题。基本上,我有一个需要验证的文本字段,然后才能持续到下一页。我以前使用过验证,但是我写这个页面时没有考虑验证(出于某种不敬虔的原因)。
这是模型页面:
class Certificate
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
attr_accessor :lot, :part_number, :quality_selected, :include_l
validates :lot, :part_number, :quality_selected, :presence => true
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
def persisted?
false
end
end
风景:
<%= form_for @certificate, :url => '/lot/certificate_print' do |f| %>
<% if @certificate.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@certificate.errors.count, "error") %> prohibited this quality from being saved:</h2>
<ul>
<% @certificate.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="modal">
<div class="dialog" style="height:380px;">
<div class="wrapper">
<h4>Prepare Certificate of Analysis</h4>
<p style="height:60px;overflow:hidden">Please enter 5 digit part number and select L* value if required by customer.</p>
<div class="form-container" style="background:#ffffff;">
<div class="wrapper">
<div class="group-field">
<div class="field" style="margin:0 10px 0 0;min-width:120px;">
<%= f.label :part_number, 'Part Number' %><br />
<%= f.text_field :part_number, {:style => 'text-align:center;width:200px;'} %>
</div>
<div class="field" style="margin:34px 10px 0 17px;min-width:120px;">
<%= check_box_tag 'certificate[include_l]' %>
<%= f.label :include_l, 'Include L*?' %>
</div>
</div>
</div>
<%= hidden_field_tag :certificate_quality_selected, params[:quality_to_include], { :name => 'certificate[quality_selected]' } %>
</div>
<div class="actions">
<a href="#" class="back-link">Back</a> <%= button_tag 'Generate', :type => 'button', :id => 'print-button' %>
</div>
</div>
</div>
</div>
<% end %>
我知道这不习惯,我只是不完全确定我应该从这里采取的方法。如果有人能对此有所了解,将不胜感激。
编辑:这是控制器,
class LotController < ApplicationController
before_filter :authenticate_user!
before_filter :check_permission
def certificate
@lots = Quality.lots.all.map { |l| [l.lot, l.lot] }
@certificate = Certificate.new
respond_to do |format|
format.html
format.js
end
end
def certificate_review
@qualities = Quality.find(params[:quality_to_include].split(','))
respond_to do |format|
format.html { render action: "certificate_review", :layout => false }
end
end
def certificate_print
@qualities = Quality.find(params[:certificate][:quality_selected].split(','))
@quality_lots = Quality.group('lot').find_all_by_id(params[:certificate][:quality_selected].split(','))
@quality_lots_list = @quality_lots.map(&:lot)
@certificate = Certificate.new(params[:certificate])
@average_moisture = Quality.where('id IN (?)',@qualities).average(:last_moisture_value)
@average_viscosity = Quality.where('id IN (?)',@qualities).average(:last_viscosity_value)
@average_color_l = Quality.where('id IN (?)',@qualities).average(:last_color_l_value)
Quality.update_all('coa_printed = "true"', 'id IN (' + params[:certificate][:quality_selected] + ')')
respond_to do |format|
format.html { render action: "certificate_print", :layout => 'blank' }
end
end
def certificate_generate
@certificate = Certificate.new
respond_to do |format|
format.html { render action: "certificate_generate", :layout => false }
end
end
private
def check_permission #checks permissions for the reporting section
if current_user.labSupervisor?
#allow past
elsif !current_user.administrator?
flash[:alert] = 'Your account must have the Administrator roles to access Certificate functionality.';
redirect_to '/'
end
end
end