我正在尝试验证字段名称custom_link的唯一性。但它允许在该字段中添加相同的值。 以下是我的模型:唯一性验证不起作用
class Gallery < ActiveRecord::Base
....
validates :custom_link, uniqueness: true
....
end
这是我的控制器代码:
def update
respond_to do |format|
if @gallery.update(gallery_params)
format.html { redirect_to galleries_galleryhome_path(id: params[:id]), notice: 'Gallery was successfully updated.' }
format.json { render :show, status: :ok, location: @gallery }
else
format.html { render :edit }
format.json { render json: @gallery.errors, status: :unprocessable_entity }
end
end end
private:
def gallery_params
params.require(:gallery).permit(:name, :shoot_date, :release_date, :expiration_date, :archive,:gallery_layout_id, :contact_id,:status, :cover_url,:gallery_photo_id, photos_attributes: [:image]).merge(brand_id: current_brand.id,user_id: current_user.id)
end
获取不知道为什么它不工作。谁能帮我?
你阅读有关的文档'唯一性'可以和不可以做? – Zabba
嗨,你可以请你分享你的控制器代码,因为你已经写了它应该工作的写入语法。 –
@Bharat Soni,我已经分享了我的控制器的代码 – Vishal