Rails Active Record validates_precense_of不闪烁错误消息

时间:2015-08-05 20:46:14

标签: ruby-on-rails ruby-on-rails-3 validation model null

我在rails应用程序中有一个文本框,我希望在保存到db之前验证输入的伪装。所以,我尝试在正确的模型中放置validates_precense_of回调(如下所示)。

class Suggestion < ActiveRecord::Base
  attr_accessible :details, :metadata, :suggestible, :user
  belongs_to :suggestable, polymorphic: true
  belongs_to :user
  serialize :metadata, JSON
  validates_presence_of :details
end

请求失败并且闪烁错误消息,请求成功,并且建议表中没有保存任何建议记录。

我已经调试并确认(请参阅下面的控制器)@suggestion.details.blank?.empty返回true,文本框为空。

这是控制器动作:

  def select_patients
    @rx_search_request = RxSearchRequest.find(params[:id])
    @groups = []
    params['selected_patient_group'].each do |id, selected|
      @groups << id if selected == "true"
    end

      unless @groups.blank?

        @rx_search_request.select_multiple_patient_group_ids(@groups)

        unless @rx_search_request.approval_status_reason_patients_and_approval? ||
          @rx_search_request.approval_status_reason_requires_approval?
          @rx_search_request.needs_consolidation!
          # @rx_search_request.approve! approved_by: current_user
          @redirect_url = rx_search_request_path(@rx_search_request)
          # @message = "Request has been created."
        else
          @message = "Request has been forwarded to your admin for approval."
        end

      end
    if params.keys.include? "suggestion"
      #we are submitting a suggestion
      group_ids = params[:selected_patient_group].collect{|k,v| k if v == 'true'}.compact
      metadata = {
        group_ids:group_ids,
        patient_ids:ManualConsolidationPatientGroup.find(group_ids).collect{|g| g.manual_consolidation_patients}.flatten.collect{|_p| _p.id}
      }

      @suggestion = @rx_search_request.suggestions.new({
        details:params[:suggestion_box],
        metadata: metadata
      })
      @suggestion.user = current_user
      @suggestion.save
      # @message = "Your suggestion has been submitted."
      # debugger
      # flash[:alert] = 'Your suggestion cannot be blank' if @suggestion.details.empty?
      flash[:alert] = 'Your suggestion has been submitted.'
    end

    respond_to do |format|
      format.js
    end
  end

将控制器更改为此

unless @suggestion.details.blank?
        flash[:alert] = 'Your suggestion has been submitted.'
      else
        flash[:alert] = 'Your suggestion cannot be blank'
      end
      #debugger
      @suggestion.save!

也试过这个

if @suggestion.save!
        flash[:alert] = 'Your suggestion has been submitted.'
      else
        flash[:alert] = 'Your suggestion cannot be blank'
      end

解决方案

添加了爆炸操作员!以进行正确的保存

1 个答案:

答案 0 :(得分:1)

您需要检查save来电的结果,或者使其save!代替请求失败:

if @suggestion.save
  flash[:alert] = 'Your suggestion has been submitted.'
else
  flash[:alert] = 'Your suggestion cannot be blank'
end

如果您希望在AJAX请求后显示验证错误,则需要在<controller_name>.js.erb文件中添加以下内容:

<% flash.each do |type, message| %>
  $("#your_element_id").html("<%= type.to_s.humanize %>: <%= message.html_safe %>")
<% end %>

...您应该将#your_element_id更改为您在其中呈现Flash消息的页面上元素的实际HTML ID。