1

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>&nbsp;&nbsp;&nbsp;<%= 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
4

1 回答 1

3

所以你有模型和视图。最后一部分通常是控制器。你需要:

  1. 在呈现表单的操作中,您需要分配给@certificate

    @certificate = Certificate.new

  2. 您需要将按钮类型更改为submit,目前为button.

  3. 在接收提交表单的操作中,您需要验证参数

    @certificate = Certificate.new(params[:certificate])

    @certificate.valid?

您应该根据验证结果决定下一步该做什么。呈现上一个操作(以便您可以打印错误)或继续。

于 2013-01-04T17:32:25.863 回答