SDe*_*Dee 5 ruby validation ruby-on-rails ruby-on-rails-4
我完全失去了这一个.我有一个Rails应用程序,用户可以在其中创建一个新的供应商,以便在系统中使用.该供应商必须具有唯一名称.因此,我们在此字段的数据库上有一个唯一索引,如下所示:
create_table "vendors", force: :cascade do |t|
t.string "vendor_name", limit: 80, null: false
t.datetime "updated_at", null: false
end
add_index "vendors", ["vendor_name"], name: "vendor_name_UNIQUE", unique: true, using: :btree
Run Code Online (Sandbox Code Playgroud)
我的问题是由于某些原因Rails验证不能解决这个问题,因此应用程序抛出了异常,因为数据库查询失败了.例外情况如下:
ActiveRecord::RecordNotUnique in VendorsController#create
Mysql2::Error: Duplicate entry '3 logiq' for key 'vendor_name_UNIQUE': INSERT INTO `vendors` (`vendor_name`, `updated_at`) VALUES ('3 logiq', '2015-12-03 21:14:12')
Run Code Online (Sandbox Code Playgroud)
vendor.rb模型文件中的My Rails验证如下:
validates :vendor_name, presence: true, uniqueness: true, length: { minimum: 5, maximum: 80 }
Run Code Online (Sandbox Code Playgroud)
我知道正在运行验证,因为我仍然收到长度和存在检查的表单错误.不确定我在这一点上能做些什么.我错过了什么吗?
以下是我的供应商控制器代码:
def create
@vendor = Vendor.new(vendor_params)
@vendor.comments.build(user_comment: comment_params[:new_comment], system_note: 'Created Vendor', user: current_user, user_agent: request.user_agent, resolution: comment_params[:screen_resolution])
respond_to do |format|
if @vendor.save
format.html { redirect_to vendors_path, notice: 'Vendor was successfully created.' }
format.json { render :show, status: :created, location: @vendor }
else
format.html { @new_item = true; render :new }
format.json { render json: @vendor.errors, status: :unprocessable_entity }
end
end
end
Run Code Online (Sandbox Code Playgroud)
供应商表单视图代码:
<% if @vendor.errors.any? %>
<div>
<div class="alert alert-danger" role="alert" id="error_explanation">
<h4><strong><%= pluralize(@vendor.errors.count, "error") %></strong> prohibited this vendor from being saved:</h4>
<ul>
<% @vendor.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
</div>
<% end %>
Run Code Online (Sandbox Code Playgroud)
这个问题实际上与区分大小写有关。解决方案是在供应商模型中的验证行中添加一小部分。
之前的验证行:
validates :vendor_name, presence: true, uniqueness: true, length: { minimum: 5, maximum: 80 }
变成:
validates :vendor_name, presence: true, uniqueness: { case_sensitive: false }, length: { minimum: 5, maximum: 80 }
注意该部分的差异uniqueness。默认情况下,Rails 确实关心区分大小写,因此在我的情况下,我的验证并不是每次都运行。
希望这对遇到此问题的其他人有所帮助!
| 归档时间: |
|
| 查看次数: |
2486 次 |
| 最近记录: |