【问题标题】:Rails 3 basic validation not working with :prompt or :include_blank?Rails 3 基本验证不适用于 :prompt 或 :include_blank?
【发布时间】:2013-02-09 23:01:00
【问题描述】:

我认为有以下代码:

<%= f.select :user_id, user_all_select_options, :include_blank => '--Select a name-----' %>

它显示一个用户列表,顶部带有--Select a name-----。当用户未从列表中选择名称并选择 --Select a name----- 时,我会收到错误,因为 user_id 为空白。

作为参考,辅助方法代码如下所示:

def user_all_select_options
  User.all.map{ |user| [user.name, user.id] }
end

我的模型如下:

class Parcel < ActiveRecord::Base

  attr_accessible :parcel, :received_at, :received_by, :chilled, :courier, :location, :passed_to, :user_id, :user, :content, :localip

  validates :user_id, :presence => true

  belongs_to :user

end

由于某种原因,验证似乎没有运行,但是如果我从下拉列表中选择一个用户并为其他字段输入添加验证,应用程序会正确地向用户显示一条消息,说明哪个字段错误地为空。

有趣的是,如果我将选择下拉菜单保留为 --Select a name----- 并保留附加验证,则会引发异常。它不会提示缺少的字段,它只是错误。

这是错误期间的记录(此记录来自我对位置字段进行验证存在检查时:

{"utf8"=>"✓", "authenticity_token"=>"wM4KPtoswp3xdv8uU4UasdadNsZi9yFZmk=", "parcel"=>{"user_id"=>"", "received_by"=>"dan", "content"=>"", "chilled"=>"0", "courier"=>"", "location"=>"", "passed_to"=>"", "received_at(3i)"=>"9", "received_at(2i)"=>"2", "received_at(1i)"=>"2013", "received_at(4i)"=>"22", "received_at(5i)"=>"59"}, "commit"=>"Receive this Parcel", "action"=>"create", "controller"=>"parcels"}

我应该从哪里开始寻找?显示的错误是控制器对用户执行除非检查。

parcel 控制器创建方法如下所示:

  def create
    @parcel = Parcel.new(params[:parcel])
    @parcel.localip = request.env['REMOTE_ADDR']
    @parcel.received_by = @parcel.received_by.upcase

    unless @parcel.user.mobilenumber.blank?
      UserMailer.parcel_notification(@parcel).deliver
    end

    respond_to do |format|
      if @parcel.save
        format.html { redirect_to @parcel, notice: 'Parcel was successfully created.' }
        format.json { render json: @parcel, status: :created, location: @parcel }
      else
        format.html { render action: "new" }
        format.json { render json: @parcel.errors, status: :unprocessable_entity }
      end
    end
  end

【问题讨论】:

  • 你在使用 bang 方法吗? (save! 或 create!)。这些方法在验证失败时返回异常。
  • 不,我已将控制器的 create 方法添加到我的问题中。

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


【解决方案1】:

不选择用户时出现异常的原因是这一行

unless @parcel.user.mobilenumber.blank?

由于未设置user_id,因此@parcel.user 是nil,这会导致异常。 我建议你把它移到@parcel.save 块内。

def create
  @parcel = Parcel.new(params[:parcel])
  @parcel.localip = request.env['REMOTE_ADDR']
  @parcel.received_by = @parcel.received_by.upcase

  respond_to do |format|
    if @parcel.save
      unless @parcel.user.mobilenumber.blank?
        UserMailer.parcel_notification(@parcel).deliver
      end

      format.html { redirect_to @parcel, notice: 'Parcel was successfully created.' }
      format.json { render json: @parcel, status: :created, location: @parcel }
    else
      format.html { render action: "new" }
      format.json { render json: @parcel.errors, status: :unprocessable_entity }
    end
  end
end

【讨论】:

    猜你喜欢
    • 2018-04-19
    • 1970-01-01
    • 2020-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-15
    • 2021-01-13
    相关资源
    最近更新 更多