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
答案 0 :(得分:2)
所以你有模特和观点。最后一部分通常是控制器。你需要:
在您呈现表单的操作中,您需要分配到@certificate
@certificate = Certificate.new
您需要将按钮类型更改为submit
,目前为button
。
在收到提交表单的操作中,您需要验证参数
@certificate = Certificate.new(params[:certificate])
@certificate.valid?
您应根据验证结果决定下一步做什么。渲染上一个操作(以便您可以打印错误)或继续。