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