即使字段不为空,也验证属性是否存在抛出错误

时间:2013-01-10 22:15:25

标签: ruby-on-rails forms validation activemodel

所以我一直在使用Active模型在ruby on rails(1.9.3)上进行多步骤表单,跟随railscast#217和#219并且在验证时出现问题,即使属性字段不为空它仍然会抛出错误

1 error prohibited this wizard from being saved:
Validationattr can't be blank

我不相信它应该保存,但这可能只是与错误有关  。 (我不能使用表格宝石,必须自己写)如果您需要更多信息,请告诉我,我很乐意提供。

我的控制器代码 - (contollers / awizard_controller.rb)

class AwizardController < ApplicationController

  def new
    # New Asset Wizard
    @wizard = Awizard.new(id: 1)
    # Set session variable as initial step
    session[:wizard_step] = @wizard.current_step
  end

  def update
    @wizard = Awizard.new(id: 1) unless !@wizard.nil?
    @wizard.current_step = session[:wizard_step] unless nil

    if @wizard.valid?
      if params[:back_button]
        @wizard.previous_step
      elsif @wizard.last_step?
        @wizard.save if @wizard.all_valid?
      else
        @wizard.next_step
      end
      session[:wizard_step] = @wizard.current_step
    end

    if @wizard.changed?
      render 'form'
    else
      @wizard.save
    end
  end

end

我的awizard模型 - (models / awizard.rb)

 class Awizard < Wizard
 validates :validationattr, :presence => true
 attr_accessor :validationattr

  def steps
    %w[validate_schedule validate_assets save_assets]
  end

 end

我的向导模型 - (models / wizard.rb)

class Wizard
include ActiveModel::Validations
include ActiveModel::Conversion
include ActiveModel::Dirty
include ActiveModel::Naming

#This class is used to manage the wizard steps using ActiveModel (not ActiveRecord)

attr_accessor :id
attr_writer :current_step  #used to write to current step
define_attribute_methods [:current_step] #used for marking change

def initialize(attributes = {})
   attributes.each do |name, value|
     send("#{name}=", value)
   end
end

def current_step
  @current_step || steps.first
end

def steps
  %w[] 
end

def next_step
  current_step_will_change! #mark changed when moving stepped
  self.current_step = steps[steps.index(current_step)+1] unless last_step?
end

def previous_step
  current_step_will_change! #mark changed when moving stepped
  self.current_step = steps[steps.index(current_step)-1] unless first_step?
end

def first_step?
  current_step == steps.first
end

def last_step?
  current_step == steps.last
end

def all_valid?
  steps.all? do |step|
    self.current_step = step
    valid?
  end
end

def step(val)
  current_step_will_change!
  self.current_step = steps[val]
end

def persisted?
  self.id == 1
end

end

我的观点(/视图/ awizard / _form.html.erb)

<%=  content_for :awizard_form do%>
  <%= form_for(@wizard) do |f| %>
    <%= render "#{@wizard.current_step}_step", :f => f %>
    <%= f.submit "Previous", :name => "back_button" unless @wizard.first_step? %>
    <%= f.submit "Continue", :name => "step" unless @wizard.last_step? %>
  <% end %>
<% end %>

(适用/视图/ awizard / _step1.html.erb)

<div class="field">
  <%= f.label :validationattr %><br />
  <%= f.text_field :validationattr %>
</div>

(适用/视图/ awizard / _step2.html.erb)

<div class="field">
  <%= f.label 'Step2' %><br />
</div>

(适用/视图/ awizard / _step3.html.erb)

<div class="field">
  <%= f.label 'Step3' %><br />
</div>

路线代码

resources :awizard

2 个答案:

答案 0 :(得分:0)

由于第2步的validationattr属性为nil,因此抛出错误。我不确定逻辑,您可能需要将其包含在后续帖子中。

答案 1 :(得分:0)

问题是MrYoshiji说明没有参数传递到第2步,验证无法在向导中验证,在控制器中添加向导参数的合并修复了这个。