0

我在控制器中有这样的代码:

if verify_recaptcha #verify_recaptcha(:model => @order, :message => "Oh! It's error with reCAPTCHA!") && 
      respond_to do |format|
        if @order.save
          Cart.destroy(session[:cart_id])
          session[:cart_id] = nil
          OrderMailer.order_confirmation(@order, @user).deliver
          UserOrderMailer.user_order_mailer(@order, @user).deliver
          #OrderNotifier.received(@order).deliver
          pdf_link = generate_pdf(@order)
          link = "  <a href=\"#{pdf_link}\">#{I18n.t(:get_pdf)}</a>"   
          format.html { redirect_to root_url, :notice => (I18n.t(:successful_order_send)+link.to_s).html_safe}

        else
          format.html { render :action => "new", :notice => I18n.t(:error) }
          format.xml  { render :xml => @order.errors, :status => :unprocessable_entity }

        end
      end
    else
      flash[:warning] = I18n.t(:wrong_captcha)
      redirect_to :back 
    end  

在表单视图中:

  - if @user_vehicle.errors.messages.values.present?
    .warning
      - @user_vehicle.errors.messages.values.each do |msg|       
        - msg.each do  |m|
          %li= m

但在布局中我也有:

- if flash[:warning]
              .warning
                = flash[:warning]
            - if flash[:notice]
              .notice
                = flash[:notice]

我想知道,我怎样才能将recaptcha 失败错误附加到errors.messages.values 错误列表并显示在同一个div 中,并像我对模型验证消息所做的那样与li 一起显示?我如何才能看到更多 li item recaptcha 消息?

4

1 回答 1

2
if verify_recaptcha
  ...
else
  @user_vehicle.errors.add(:base, I18n.t(:wrong_captcha))
end
于 2013-01-11T12:17:39.797 回答