R. *_*eff 6 validation nested-forms multipage ruby-on-rails-3
我正在尝试使用设计和活跃商家编写注册.表单很复杂,我的用户对象如下所示:
class User < ActiveRecord::Base
include ActiveMerchant::Utils
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable
# Setup accessible (or protected) attributes
attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :first_name,
:subscription_attributes, :last_name, :zipcode,
:payment_profile_attributes, :customer_cim_id, :payment_profile_id
...
# Track multi-page registration
attr_writer :current_step
...
# Setup Payment Profile element (authorize.net billing profile)
has_one :payment_profile, :dependent => :delete
accepts_nested_attributes_for :payment_profile
Run Code Online (Sandbox Code Playgroud)
现在,PaymentProfile类有自己的子项,来自活跃商家的两项:
require 'active_merchant'
class PaymentProfile < ActiveRecord::Base
include ActiveMerchant::Billing
include ActiveMerchant::Utils
validate_on_create :validate_card, :validate_address
attr_accessor :credit_card, :address
belongs_to :user
validates_presence_of :address, :credit_card
def validate_address
unless address.valid?
address.errors.each do |error|
errors.add( :base, error )
end
end
end
def address
@address ||= ActiveMerchant::Billing::Address.new(
:name => last_name,
:address1 => address1,
:city => city,
:state => state,
:zip => zipcode,
:country => country,
:phone => phone
)
end
def validate_card
unless credit_card.valid?
credit_card.errors.full_messages.each do |message|
errors.add( :base, message )
end
end
end
def credit_card
@credit_card ||= ActiveMerchant::Billing::CreditCard.new(
:type => card_type,
:number => card_number,
:verification_value => verification_code,
:first_name => first_name,
:last_name => last_name
)
@credit_card.month ||= card_expire_on.month unless card_expire_on.nil?
@credit_card.year ||= card_expire_on.year unless card_expire_on.nil?
return @credit_card
end
Run Code Online (Sandbox Code Playgroud)
现在我已经覆盖了Devise的RegistrationsController,使用Ryan Bates多页表格截屏(http://railscasts.com/episodes/217-multistep-forms)的解决方案处理多页表单.我不得不调整它以使其与Devise合作,但我成功了.现在因为Ryan的多页表单只是在不同的页面上询问了相同模型的不同字段,所以他能够valid?通过在他的validate方法中添加:if块来覆盖他的方法a la:
validates_presence_of :username, :if => lambda { |o| o.current_step == "account" }
Run Code Online (Sandbox Code Playgroud)
但在我的情况下,我要求从我的父模型(用户)获取第一个表单上的所有字段,然后询问我的两个孙子模型中的所有字段(用户:PaymentProfile:地址,用户:PaymentProfile:Credit_Card )在第二页.
我面临的问题是虽然PaymentProfile.valid?根据ActiveMerchant的逻辑返回错误,表单本身不呈现甚至显示这些错误.结算页面的查看代码如下所示:
<h2>Payment Details</h2>
<%= semantic_form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<%= f.semantic_fields_for :payment_profile do |p| %>
<%= p.semantic_fields_for :address do |a| %>
<%= a.inputs "Billing Information", :id => "billing" do %>
<%= a.input :type, :label => "Credit Card", :as => :select, :collection => get_creditcards %>
<%= a.input :number, :label => "Card Number", :as => :numeric %>
<%= a.input :card_expire_on, :as => :date, :discard_day => true, :start_year => Date.today.year, :end_year => (Date.today.year+10), :add_month_numbers => true %>
<%= a.input :first_name %>
<%= a.input :last_name %>
<%= a.input :verification_code, :label => "CVV Code" %>
<% end %>
<% end %>
<%= f.semantic_fields_for :credit_card do |c| %>
<%= c.inputs "Billing Address", :id => "address" do %>
<%= c.input :address1, :label => "Address" %>
<%= c.input :city %>
<%= c.input :state, :as => :select, :collection => Carmen::states %>
<%= c.input :country, :as => :select, :collection => Carmen::countries, :selected => 'US' %>
<%= c.input :zipcode, :label => "Postal Code" %>
<%= c.input :phone, :as => :phone %>
<% end %>
<% end %>
<% end %>
<%= f.commit_button :label => "Continue" %>
<% unless @user.first_step? %>
<%= f.commit_button :label => "Back", :button_html => { :name => "back_button" } %>
<% end %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
我puts errors在有效的代码后面的代码中添加了一条消息?命令,它显示如下:
{:base=>[["first_name", ["cannot be empty"]], ["last_name", ["cannot be empty"]], ["year", ["expired", "is not a valid year"]], ["type", ["is required", "is invalid"]], ["number", ["is not a valid credit card number"]], ["verification_value", ["is required"]], ["address1", ["is required"]], ["city", ["is required"]], ["state", ["is required"]], ["zip", ["is required", "must be a five digit number"]], ["phone", ["is required", "must be in the format of 333-333-3333"]]]}
{:base=>[["first_name", ["cannot be empty"]], ["last_name", ["cannot be empty"]], ["year", ["expired", "is not a valid year"]], ["type", ["is required", "is invalid"]], ["number", ["is not a valid credit card number"]], ["verification_value", ["is required"]], ["address1", ["is required"]], ["city", ["is required"]], ["state", ["is required"]], ["zip", ["is required", "must be a five digit number"]], ["phone", ["is required", "must be in the format of 333-333-3333"]]]}
Run Code Online (Sandbox Code Playgroud)
现在,此输出的结构与标准错误输出的输出不匹配,标准错误输出是由单层哈希构建的,例如:
{:username=>["can't be blank"]}
Run Code Online (Sandbox Code Playgroud)
所以在向您展示了所有这些之后,我的问题是:a)如何正确显示错误输出以便表单实际将它们吐出来?b)如何防止parent.valid?从验证孙子.有效?当我不在那个页面上?我不能在子模型上使用:if => lambda ...解决方案,因为他们不知道current_step是什么.
我为这么长的帖子道歉,我只想尽可能多地提供信息.我已经和我一起挣扎了一个星期了,我似乎无法超越它.任何建议都会非常有帮助.提前致谢.
错误未显示的原因可能是它们是在基础上填充的,而不是在单个属性上填充的。这发生在您的validate_card和validate_address方法中。您应该将错误添加到导致错误的特定属性,而不是将错误添加到基础中。
errors.add( attr , error )
Run Code Online (Sandbox Code Playgroud)
其次,如果您想让验证依赖于某个状态,如您提到的截屏视频,那么您需要将状态标志与模型一起保存(可能最好是父级)。您可以手动执行此操作,或者更好的是,您可以使用 gem 来执行此操作(推荐):state_machine
祝你好运。
| 归档时间: |
|
| 查看次数: |
1289 次 |
| 最近记录: |