使用redirect_to和error_messages_for

时间:2014-01-27 21:18:09

标签: ruby-on-rails

如何在下面的示例中显示错误消息以显示在error_messages_for中?

在控制器中创建方法:

respond_to do |format|
  if @enquiry.save && @enquiry.valid?
    flash[:notice] = t('enquiry.messages.creation_success')
    format.html { redirect_to(@enquiry) }
    format.xml { render :xml => @enquiry, :status => :created, :location => @enquiry }
    format.json {
      render :json => @enquiry.compact.to_json
    }
  else
    format.html {
      @form_sections = get_form_sections
      render :action => 'new'
      }
      format.xml { render :xml => @enquiry.errors, :status => :unprocessable_entity }
  end
end

为了寻找新的:

<%= error_messages_for :enquiry %>

在我的模特中,我有:

validates_presence_of :enquirer_name, :message =>   I18n.t("errors.models.enquiry.presence_of_enquirer_name")
validates_presence_of :criteria, :message => I18n.t("errors.models.enquiry.presence_of_criteria")

0 个答案:

没有答案