当我更新我的表单时,它会为字段抛出唯一的错误。
在我的模特中
validates :url, presence: true, uniqueness: true, :if => lambda {self.role_id == 2}
以我的形式
= f.text_field :url, :class => 'form-control'
在我的控制器中
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to stores_path, notice: 'Store was successfully updated.' }
else
format.html { render :edit }
end
end
end
即使我没有做任何事情,也会给我错误"这个网址已被采取"。请帮忙 提前致谢
答案 0 :(得分:0)
我这样解决了。
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to stores_path, notice: 'Store was successfully updated.' }
else
incoming_url = User.where("id=? and url=?",@user.id,params[:user][:url])
if !incoming_url.blank?
format.html { redirect_to stores_path, notice: 'Store was successfully updated.' }
end
format.html { render :edit }
end
end
end